Reputation: 3527
I have a test with failing despite knowing the functionality works in the app. My instinct says that I should try saving the thing that I create but I'm not sure how to do this in the assert_difference block beacause it doesn't look like the new thing
is assigned to a variable on which I can .save
. Thanks for any advice you can provide.
Test:
test "should create thing" do
assert_difference('thing.count') do
post :create, thing: { thing_type_id: @thing.thing_type_id, name: @thing.name}
end
Output:
1) Failure:
test_should_create_thing(thingsControllerTest) [C:/../thing_controller_test.rb:20]:
"thing.count" didn't change by 1.
<3> expected but was
<2>.
Upvotes: 0
Views: 53
Reputation: 6961
Sounds like you may have some left over state in your database. I see that expected but was <2>
, meaning you already have two Thing
s in your DB.
You can try clearing the DB state between tests. Depending on your database check out the database_cleaner
gem.
Also, it seems you may have already created the object, by the existence of @thing
. If that is the case, this is working as expected.
You can take the controller out of the equation to verify this by just testing a normal Thing::create
:
test "creates a new Thing" do
assert_difference('Thing.count') do
Thing.create thing_type_id: @thing.thing_type_id, name: @thing.name
end
end
Upvotes: 2