Tom Hoen
2008-Nov-03 15:23 UTC
[rspec-users] Expected "with" matches received "with" in error message
I am trying to spec a controller that sets up a page to add two instances of an object to another (many to many :through). In my controller_spec, I have the following statements: Student.should_receive(:new).and_return(mock_student) mock_student.should_receive(:relatives).twice mock_student.relatives.should_receive("<<").with(Relative.new(:parent => Parent.new)).twice When I run the test, I get the following error message: Spec::Mocks::MockExpectationError in ''StudentsController responding to GET new should expose a new student as @student'' Mock ''NilClass'' expected :<< with (#<Relative id: nil, parent_id: nil, student_id: nil, created_at: nil, updated_at: nil, child_lives_with: nil>) but received it with (#<Relative id: nil, parent_id: nil, student_id: nil, created_at: nil, updated_at: nil, child_lives_with: nil>) It seems what it is receiving and expecting are the same. Any ideas would be greatly appreciated. Tom -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2008-Nov-03 15:30 UTC
[rspec-users] Expected "with" matches received "with" in error message
On Mon, Nov 3, 2008 at 9:23 AM, Tom Hoen <lists at ruby-forum.com> wrote:> I am trying to spec a controller that sets up a page to add two > instances of an object to another (many to many :through). > > In my controller_spec, I have the following statements: > > Student.should_receive(:new).and_return(mock_student) > mock_student.should_receive(:relatives).twice > mock_student.relatives.should_receive("<<").with(Relative.new(:parent > => Parent.new)).twice > > When I run the test, I get the following error message: > > Spec::Mocks::MockExpectationError in ''StudentsController responding to > GET new should expose a new student as @student'' > Mock ''NilClass'' expected :<< with (#<Relative id: nil, parent_id: nil, > student_id: nil, created_at: nil, updated_at: nil, child_lives_with: > nil>) but received it with (#<Relative id: nil, parent_id: nil, > student_id: nil, created_at: nil, updated_at: nil, child_lives_with: > nil>) > > It seems what it is receiving and expecting are the same. > > Any ideas would be greatly appreciated.ActiveRecord treats two unsaved records as unequal, even if they have the same properties. Try this in script/console Relative.new == Relative.new => false To get this to work you''ll have to either use real records (with IDs) or stub whatever it is that is supplying the Relative and then expect with(the_relative_you_supply_as_a_stub_value). Make sense?> > Tom > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Pat Maddox
2008-Nov-03 15:50 UTC
[rspec-users] Expected "with" matches received "with" in error message
"David Chelimsky" <dchelimsky at gmail.com> writes:> On Mon, Nov 3, 2008 at 9:23 AM, Tom Hoen <lists at ruby-forum.com> wrote: >> I am trying to spec a controller that sets up a page to add two >> instances of an object to another (many to many :through). >> >> In my controller_spec, I have the following statements: >> >> Student.should_receive(:new).and_return(mock_student) >> mock_student.should_receive(:relatives).twice >> mock_student.relatives.should_receive("<<").with(Relative.new(:parent >> => Parent.new)).twice >> >> When I run the test, I get the following error message: >> >> Spec::Mocks::MockExpectationError in ''StudentsController responding to >> GET new should expose a new student as @student'' >> Mock ''NilClass'' expected :<< with (#<Relative id: nil, parent_id: nil, >> student_id: nil, created_at: nil, updated_at: nil, child_lives_with: >> nil>) but received it with (#<Relative id: nil, parent_id: nil, >> student_id: nil, created_at: nil, updated_at: nil, child_lives_with: >> nil>) >> >> It seems what it is receiving and expecting are the same. >> >> Any ideas would be greatly appreciated. > > ActiveRecord treats two unsaved records as unequal, even if they have > the same properties. Try this in script/console > > Relative.new == Relative.new > => false > > To get this to work you''ll have to either use real records (with IDs) > or stub whatever it is that is supplying the Relative and then expect > with(the_relative_you_supply_as_a_stub_value). > > Make sense?Also I''d like to bring attention to "Mock ''NilClass'' expected :<<". mock_student.relatives is nil, but you''ll want to return something. As for the actual problem, what David said :) Pat
Tom Hoen
2008-Nov-03 16:01 UTC
[rspec-users] Expected "with" matches received "with" in error message
> > To get this to work you''ll have to either use real records (with IDs) > or stub whatever it is that is supplying the Relative and then expect > with(the_relative_you_supply_as_a_stub_value). > > Make sense?In part, but not 100%. Here is the code from the controller. Since all objects are expected to be new, I am not clear whether using real objects would create a false test. I am quite new to rspec, and admittedly don''t fully grasp when to mock and when to not. @student = Student.new 2.times { @student.relatives << Relative.new(:parent => Parent.new) } -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2008-Nov-03 16:44 UTC
[rspec-users] Expected "with" matches received "with" in error message
On Mon, Nov 3, 2008 at 9:30 AM, David Chelimsky <dchelimsky at gmail.com> wrote:> On Mon, Nov 3, 2008 at 9:23 AM, Tom Hoen <lists at ruby-forum.com> wrote: >> I am trying to spec a controller that sets up a page to add two >> instances of an object to another (many to many :through). >> >> In my controller_spec, I have the following statements: >> >> Student.should_receive(:new).and_return(mock_student) >> mock_student.should_receive(:relatives).twice >> mock_student.relatives.should_receive("<<").with(Relative.new(:parent >> => Parent.new)).twice >> >> When I run the test, I get the following error message: >> >> Spec::Mocks::MockExpectationError in ''StudentsController responding to >> GET new should expose a new student as @student'' >> Mock ''NilClass'' expected :<< with (#<Relative id: nil, parent_id: nil, >> student_id: nil, created_at: nil, updated_at: nil, child_lives_with: >> nil>) but received it with (#<Relative id: nil, parent_id: nil, >> student_id: nil, created_at: nil, updated_at: nil, child_lives_with: >> nil>) >> >> It seems what it is receiving and expecting are the same. >> >> Any ideas would be greatly appreciated. > > ActiveRecord treats two unsaved records as unequal, even if they have > the same properties. Try this in script/console > > Relative.new == Relative.new > => falseThis fact has always bugged me, so I finally did something about it: http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1321 http://github.com/dchelimsky/rails/commit/03be483 Cheers, David> > To get this to work you''ll have to either use real records (with IDs) > or stub whatever it is that is supplying the Relative and then expect > with(the_relative_you_supply_as_a_stub_value). > > Make sense? > >> >> Tom >> -- >> Posted via http://www.ruby-forum.com/. >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> >