How do I create a new object in testing to test a controller?

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[:notice] = “ok, it exists”
else[:notice] = “it does not exist”
render :new

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 is correct
assert user.persisted? # make sure it's persisted

post :create, post: { email: "[email protected]" }

assert_equal "ok, it exists",[:notice]


And this is the execution flow:

$ bundle exec rake db:test:prepare
$ ruby -Itest test/functional/sessions_controller_test.rb -n
Run options: -n test_object_creation

Running tests:


Finished tests in 0.235903s, 4.2390 tests/s, 12.7171 assertions/s.

  1. Failure:
    <“ok, it exists”> expected but was
    <“it does not exist”>.

1 tests, 1 assertions, 1 failures, 0 errors, 0 skips

It seems that is not persisting to the database. But why? I
asserted the persistion and it passes.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs