I’m using Test::Unit to do functional testing on one of my controllers.
The action I want to test is create, from SessionsController. I have
simplified it to the extreme:
def create
@user = User.find_by_email(params[:email])
if @user
flash.now[:notice] = “ok, it exists”
else
flash.now[:notice] = “it does not exist”
end
render :new
end
This is my functional test:
require ‘test_helper’
class SessionsControllerTest < ActionController::TestCase
test “object creation” do
user = User.create!(email: “[email protected]”, password: “secret”)
assert user.valid? # make sure User.new is correct
assert user.persisted? # make sure it's persisted
post :create, post: { email: "[email protected]" }
assert_equal "ok, it exists", flash.now[:notice]
end
end
And this is the execution flow:
$ bundle exec rake db:test:prepare
$ ruby -Itest test/functional/sessions_controller_test.rb -n
test_object_creation
Run options: -n test_object_creation
Running tests:
F
Finished tests in 0.235903s, 4.2390 tests/s, 12.7171 assertions/s.
- Failure:
test_object_creation(SessionsControllerTest)
[test/functional/sessions_controller_test.rb:13]:
<“ok, it exists”> expected but was
<“it does not exist”>.
1 tests, 1 assertions, 1 failures, 0 errors, 0 skips
It seems that User.new is not persisting to the database. But why? I
have
asserted the persistion and it passes.