0

I have an ActionController::TestCase that's failing and I don't know why.

require 'test_helper'
class UsersControllerTest < ActionController::TestCase

  setup do
    @user = users(:bob)
    sign_in @user
  end

  test "should create user" do
    assert_difference('User.count') do
      post :create, user: { 
        first_name: 'Alice',
        email: 'alice@example.com',
        password: 'adflihbrgshbart'
      }
    end
  end
end

The result is

Failure:
UsersControllerTest#test_should_create_user [.../dev/test/controllers/users_controller_test.rb:23]
Minitest::Assertion: "User.count" didn't change by 1.
Expected: 5
  Actual: 4

Is there a way to have post show me what the errors? For instance, is there a way I can access errors array to call errors.full_messages that'd normally be available in the resulting view when there's an error?

Turgs
  • 1,729
  • 1
  • 20
  • 49

1 Answers1

0

You can access the controller instance variables using assign, so it should be possible to access errors for that instance variable and test full_messages. This may not work if the reason the record wasn't created was because some before_action prevented create from being called. But you can test if that happens with the 'pry' gem and making binding.pry the first line of the create.

In any case, to examine the instance variable...

  post :create, user: { 
    first_name: 'Alice',
    email: 'alice@example.com',
    password: 'adflihbrgshbart'
  }
  assert([], assign[:user].errors.full_messages)
SteveTurczyn
  • 36,057
  • 6
  • 41
  • 53