# Count error on lambda should change

**URL:** <https://www.ruby-forum.com/t/count-error-on-lambda-should-change/226115>\
**Category:** RSpec\
**Created:** [December 14, 2010, 2:32am UTC](https://www.ruby-forum.com/t/count-error-on-lambda-should-change/226115 "2010-12-14T02:32:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![djangst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ce73a5/32.png) [@djangst](https://www.ruby-forum.com/u/djangst)\
**Post date:** [December 14, 2010, 2:32am UTC](https://www.ruby-forum.com/t/count-error-on-lambda-should-change/226115/1 "2010-12-14T02:32:33Z")

</div>

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.]

---

<div class="post-metadata">

**Author:** ![djangst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ce73a5/32.png) [@djangst](https://www.ruby-forum.com/u/djangst)\
**Post date:** [December 14, 2010, 4:16am UTC](https://www.ruby-forum.com/t/count-error-on-lambda-should-change/226115/2 "2010-12-14T04:16:34Z")

</div>

On Mon, Dec 13, 2010 at 4:31 PM, djangst [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![djangst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ce73a5/32.png) [@djangst](https://www.ruby-forum.com/u/djangst)\
**Post date:** [December 14, 2010, 7:32pm UTC](https://www.ruby-forum.com/t/count-error-on-lambda-should-change/226115/3 "2010-12-14T19:32:19Z")

</div>

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.
