2013-01-01 2 views
2

내 모델에 코드가 있습니다. 테스트 후 rspec와 after_create

class Foo < ActiveRecord::Base 
after_create :create_node_for_foo 

    def create_node_for_user 
    FooBar.create(id: self.id) 
    end 
end 

및 푸 모델

describe Foo do 

    let (:foo) {FactoryGirl.create(:foo)} 

    subject { foo } 
    it { should respond_to(:email) } 
    it { should respond_to(:fullname) } 

it "have mass assignable attributes" do 
    foo.should allow_mass_assignment_of :email 
    foo.should allow_mass_assignment_of :fullname 
end 

it "create node in graph database" do 
    foo1 = FactoryGirl.create(:foo) 
    FooBar.should_receive(:create).with(id: foo1.id) 
end 
end 

의 RSpec에 코드를 가지고 있지만 내 테스트 잘못 될 일을

Failures: 

    1) Foo create node in graph database on 
     Failure/Error: FooBar.should_receive(:create).with(id: foo1.id) 
     (<FooBar (class)>).create({:id=>18}) 
     expected: 1 time 
     received: 0 times 

메시지

과 실패? 좋아

답변

3

이 문제

주변에있어

it "create node in graph database" do 
    foo1 = FactoryGirl.build(:foo) 
    FooBar.should_receive(:create).with(id: foo1.id) 
    foo1.save 
end 
+2

왜 변경 되었습니까? 나는 당신이 가지고있는 것과 똑같은 내 테스트를 리팩터링했지만 여전히 효과가 없었다. 왜 그리고/또는 RSpec이 무대 뒤에서 어떤 아이디어를 가지고 있는가? –

1

파티에 조금 늦게이

it "create node in graph database" do 
    foo1 = FactoryGirl.create(:foo) 
    FooBar.should_receive(:create).with(id: foo1.id) 
end 

을 변경하지만, 실제로는 위에서 작동하지 않습니다. 당신은 그것을 할 수있는 사용자 정의 정규를 만들 수 있습니다 귀하의 사양에 그런

class EventualValueMatcher 
    def initialize(&block) 
    @block = block 
    end 

    def ==(value) 
    @block.call == value 
    end 
end 

def eventual_value(&block) 
    EventualValueMatcher.new(&block) 
end 

할 :

it "create node in graph database" do 
    foo1 = FactoryGirl.build(:foo) 
    FooBar.should_receive(:create).with(eventual_value { { id: foo1.id } }) 
    foo1.save 
end 

이 모의는 사실이 끝날 때까지 블록을 평가하지 않음을 의미하며 실제로 설정되어있는 .