0

"Charity"라는 개체가 있고 각 자선 단체에 고유 한 이름이 있는지 확인하고 싶습니다. 불행히도 다음과 같은 테스트가 실패합니다. 내 공장에 문제가 있다고 생각합니다.정의되지 않은 메서드 'valid?'로 실패한 개체 특성 고유성에 대한 Rspec 테스트 NilClass

Failures 
    1) Charity name is already taken 
     Failure/Error: it { @charity_with_same_name.should_not be_valid } 
     NoMethodError: 
     undefined method 'valid?' for nil:NilClass 
     ... 

자선 개체는 사용자 개체에 속합니다. 여기

class Charity < ActiveRecord::Base 
    attr_accessible :name, :description, :summary 
    belongs_to :user 

    validates :name, presence: true, uniqueness: { case_sensitive: false }, length: { maximum: 40 } 
    validates :summary, presence: true, length: { maximum: 140 } 
    validates :description, presence: true 
    validates :user_id, presence: true 

    default_scope order: 'charities.created_at DESC' 
end 

이 charity_spec.rb의 관련 부분은 다음과 같습니다 : 여기

require 'spec_helper' 

describe Charity do 

    let(:user) { FactoryGirl.create(:user) } 
    #before { @charity = user.charities.build(summary: "Lorem ipsum") } 
    before { @charity = user.charities.build(FactoryGirl.attributes_for(:charity)) } 
    # this makes sure that all the attributes for charity are set, whereas 
    #the previous code only sets the "summary" attribute 

    subject { @charity } 
    ... 
    describe "name is already taken" do 
    before do 
     charity_with_same_name = @charity.dup 
     charity_with_same_name.name = @charity.name.upcase 
     charity_with_same_name.save 
    end 

    it { @charity_with_same_name.should_not be_valid } 
    end 
    ... 

factory.rb입니다 : 내가 무슨 짓을

FactoryGirl.define do 
    factory :user do 
    sequence(:name) { |n| "Person #{n}" } 
    sequence(:email) { |n| "person_#{n}@example.com" } 
    password "foobar" 
    password_confirmation "foobar" 
    ... 

    factory :charity do 
     sequence(:name)  { |n| "charity #{n}" } 
     sequence(:summary)  { |n| "summary #{n}" } 
     sequence(:description) { |n| "description #{n}" } 
    end 
end 

여기 charity.rb입니다 잘못된?

답변

0

당신은 전에 블록 (변경 @charity_with_same_name-charity_with_same_name)에 charity_with_same_name 인스턴스 변수를 만들고,이 통과 얻을 specify { @charity_with_same_name.should_not be_valid }it { @charity_with_same_name.should_not be_valid }을 변경할 수 있습니다.

초기화하지 않았으므로 @charity_with_same_name이 존재하지 않습니다. charity_with_same_name 만 설정했습니다. 또한 subject { @charity }이기 때문에 it@charity이므로이 경우 it { @charity_with_same_name.should_not be_valid }이 적합하지 않습니다.

편집 : 위의 작업을 추가하는 경우 블록을 before 블록 (charity_with_same_name = @charity.dup 줄 바로 앞에)에 추가해야합니다. @charity이 데이터베이스에 아직 저장되지 않았기 때문에 중복 레코드가 @charity과 같은 이름 임에도 불구하고 유효성 검사를 통과해야하므로 필요합니다. 일단 자선이 저장되면 유효성 검사 테스트는 데이터베이스를 검사하고 이름이 이미 사용 된 것을 확인합니다.

+0

감사합니다.하지만 charity_with_same_name의 각 인스턴스에 @를 추가하고 "지정"으로 바꾸면 실패합니다. { "at"charity_with_same_name.should_not be_valid}이 (가) 유효 할 것으로 예상됩니까? 거짓을 돌려 주려면 진실합니다. 실패/오류 : charity_with_same_name.name = "at"charity.name.upcase, NameError : 정의되지 않은 지역 변수 또는 메서드 'charity_with_same_name'. 오류를 반환하는 첫 번째 인스턴스에 @를 추가합니다. .. 내가 뭐 잘못 했어요? 그래서 내가 "at"로 바꿔서 @를 사용하게하지 않을 것입니다. – user1424388

+0

@ user1424388 제 편집을 참조하십시오. – cdesrosiers

+0

그랬어! 고마워. – user1424388