jg
2009-Aug-24 23:09 UTC
[rspec-users] Assigning instance variable :expected and :got appear to be the same
I have a very fairly simple spec it "should assign an instance variable" do get ''new'' assigns[:city].should == City.new end In my controller def new @city = City.new end but when I run it, I get ''CitiesController GET ''new'' should assign an instance variable'' FAILED expected: #<City id: nil, name: nil, created_at: nil, updated_at: nil>, got: #<City id: nil, name: nil, created_at: nil, updated_at: nil> (using ==) Those two things seem to be equal, am I wrong there?
David Chelimsky
2009-Aug-24 23:37 UTC
[rspec-users] Assigning instance variable :expected and :got appear to be the same
On Mon, Aug 24, 2009 at 6:09 PM, jg<jgagne33 at gmail.com> wrote:> I have a very fairly simple spec > > it "should assign an instance variable" do > ? get ''new'' > ? assigns[:city].should == City.new > end > > In my controller > > def new > ?@city = City.new > end > > but when I run it, I get > > ''CitiesController GET ''new'' should assign an instance variable'' FAILED > expected: #<City id: nil, name: nil, created_at: nil, updated_at: > nil>, > ? ? ? ? got: #<City id: nil, name: nil, created_at: nil, updated_at: > nil> (using ==) > > Those two things seem to be equal, am I wrong there?According to ActiveRecord, two objects with nil IDs are not considered to be equal. Try this in script/console and you''ll see: $ City.new == City.new => false HTH, David
Christoph Schiessl
2009-Aug-25 09:09 UTC
[rspec-users] Assigning instance variable :expected and :got appear to be the same
On Aug 25, 2009, at 1:09 AM, jg wrote:> I have a very fairly simple spec > > it "should assign an instance variable" do > get ''new'' > assigns[:city].should == City.new > end > > In my controller > > def new > @city = City.new > end > > but when I run it, I get > > ''CitiesController GET ''new'' should assign an instance variable'' FAILED > expected: #<City id: nil, name: nil, created_at: nil, updated_at: > nil>, > got: #<City id: nil, name: nil, created_at: nil, updated_at: > nil> (using ==) > > Those two things seem to be equal, am I wrong there? > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersYour controller is okay. Try to change your test like this: it "should assign an instance variable" do City.should_receive(:new).and_return(@city = mock_model(City)) get ''new'' assigns[:city].should == @city end That way, you are comparing the same object instance => it should pass. Best regards, Christoph
jg
2009-Aug-25 16:12 UTC
[rspec-users] Assigning instance variable :expected and :got appear to be the same
Ahh, got it! Thanks alot guys. On Aug 25, 2:09?am, Christoph Schiessl <c... at proactive.or.at> wrote:> On Aug 25, 2009, at 1:09 AM, jg wrote: > > > > > > > I have a very fairly simple spec > > > it "should assign an instance variable" do > > ? get ''new'' > > ? assigns[:city].should == City.new > > end > > > In my controller > > > def new > > ?@city = City.new > > end > > > but when I run it, I get > > > ''CitiesController GET ''new'' should assign an instance variable'' FAILED > > expected: #<City id: nil, name: nil, created_at: nil, updated_at: > > nil>, > > ? ? ? ? got: #<City id: nil, name: nil, created_at: nil, updated_at: > > nil> (using ==) > > > Those two things seem to be equal, am I wrong there? > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > Your controller is okay. > > Try to change your test like this: > > it "should assign an instance variable" do > ? ?City.should_receive(:new).and_return(@city = mock_model(City)) > ? ?get ''new'' > ? ?assigns[:city].should == @city > end > > That way, you are comparing the same object instance => it should pass. > > Best regards, > Christoph > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users