Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing after_create with rspec

I have code in my model.

class Foo < ActiveRecord::Base
 after_create :create_node_for_foo

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

and have code in rspec of Foo model

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

but my test is failing with message

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

What might be wrong?

like image 402
Bhushan Lodha Avatar asked Feb 07 '26 05:02

Bhushan Lodha


1 Answers

Okay got around with problem

changed this

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

to

 it "create node in graph database" do
  foo1 = FactoryGirl.build(:foo)
  FooBar.should_receive(:create).with(id: foo1.id)
  foo1.save
end
like image 172
Bhushan Lodha Avatar answered Feb 12 '26 03:02

Bhushan Lodha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!