Hi, any idea what could cause the following error in a controller test? "count should have been changed by 1, but was changed by 0" I''m posting to a create route using a lambda, similar to Hartl''s example in his Rails tutorial: it "should create a user" do lambda do post :create, :user => @attr end.should change(User, :count).by(1) end The only difference is I''m using a factory instead of an attribute hash. I know the action works because I can perform it manually and my model specs pass. More importantly, I can see the inserts (two rows, parent and child) in my Postgres logs. And I see a rollback before the count is selected. Could that be the cause? [I searched for a while and didn''t see much discussion about this, so probably not.]
Hi, any idea what could cause the following error in a controller test? "count should have been changed by 1, but was changed by 0" I''m posting to a create route using a lambda, similar to Hartl''s example in his Rails tutorial: it "should create a user" do lambda do post :create, :user => @attr end.should change(User, :count).by(1) end The only difference is I''m using a factory instead of an attribute hash. I know the action works because I can perform it manually and my model specs pass. More importantly, I can see the inserts (two rows, parent and child) in my Postgres logs. And I see a rollback before the count is selected. Could that be the cause? [I searched for a while and didn''t see much discussion about this, so probably not.]
On Mon, Dec 13, 2010 at 4:31 PM, djangst <djangst at gmail.com> wrote:> Hi, any idea what could cause the following error in a controller > test? > > ? ? "count should have been changed by 1, but was changed by 0" > > I''m posting to a create route using a lambda, similar to Hartl''s > example in his Rails tutorial: > > it "should create a user" do > ?lambda do > ? ?post :create, :user => @attr > ?end.should change(User, :count).by(1) > end > > The only difference is I''m using a factory instead of an attribute > hash. I know the action works because I can perform it manually and my > model specs pass.Please show us your code instead of referencing someone else''s and then describing the differences. There are so many things that could be the source of the problem that anything anybody offers here is going to be a guess at best.
The problem was related to the use of the factory-generated instance, in this case a parent with one child instance similar to Hartl''s user/ micropost example, but different in that I require at least one child instance to be present (in the has_many relationship). I''m doing this in conjunction with an attachment upload, so getting everything working together and tested properly has been problematic. I may post an example later to hopefully get some feedback.
Sounds like a validation failure. My guess is that you''re re-using the factory-created record''s attributes, and your example fails because of a uniqueness validation. This would explain the rollback if you''re using the create! or save! method. Pat On Dec 13, 2010, at 3:08 PM, djangst <djangst at gmail.com> wrote:> Hi, any idea what could cause the following error in a controller > test? > > "count should have been changed by 1, but was changed by 0" > > I''m posting to a create route using a lambda, similar to Hartl''s > example in his Rails tutorial: > > it "should create a user" do > lambda do > post :create, :user => @attr > end.should change(User, :count).by(1) > end > > The only difference is I''m using a factory instead of an attribute > hash. I know the action works because I can perform it manually and my > model specs pass. > > More importantly, I can see the inserts (two rows, parent and child) > in my Postgres logs. And I see a rollback before the count is > selected. Could that be the cause? [I searched for a while and didn''t > see much discussion about this, so probably not.] > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users