mercredi 22 février 2017

Undefined method '[]' for nil class during test

I'm learning how to write automated tests in rails and I'm running into a problem. Here's my test:

test "login invalido" do
    get new_usuario_path
    assert_template 'usuarios/new'
    post login_path, params: { sessao: { email: "", password: "" } }
    assert_template 'usuarios/new'
    assert_not flash.empty?
    get root_path
    assert flash.empty?
end

And here's the method in login_path:

def create
byebug
u = Usuario.find_by(email: params[:sessao][:email].downcase)
if u.present? && u.authenticate(params[:sessao][:password])
  puts "aqui"
else
    flash[:danger] = 'Ops! Email ou senha inválidos.'
    redirect_to new_usuario_path
end
end

I'm trying to verify if the flash error message is displayed and doesn't linger during navigation (I'm using this book as reference). I expected to get a failure in this test, but instead I get an error:

 1) Error:
LoginTest#test_login_invalido:
NoMethodError: undefined method `[]' for nil:NilClass
app/controllers/sessao_controller.rb:7:in `create'
test/integration/login_test.rb:7:in `block in <class:LoginTest>'

It seems that the params hash is incorrect, but when running the test with byebug I see that the hash is arriving correctly:

(byebug) params
{"params"=>{"sessao"=>{"email"=>"", "password"=>""}}, "controller"=>"sessao", "action"=>"create"}

What am I missing?

Aucun commentaire:

Enregistrer un commentaire