0

If I have a simple login function:

def login
  @user = User.find_by_email params[:email]
  if @user
    if  BCrypt::Password.new(@user.encrypted_password).is_password? params[:password]
      session[:current_user_id] = @user.id
      render json: true
    else
      render json: false
    end
  else
    render json: false
  end
end

and I want to test if this function actually will return false if user does not exist I have created the following test:

  def test_my_action
    post :login, :format => 'json',  :email => 'foo@bar.com', :password => 'test'
    assert_response json:false
  end

I know that assert_response will return status 200 or other, but I cant figure out how to create the necessary assert.

How do I test if my login function actually return false?

cweston
  • 11,297
  • 19
  • 82
  • 107
Timsen
  • 4,066
  • 10
  • 59
  • 117
  • 1
    possible duplicate of [How to check for a JSON response using RSpec?](http://stackoverflow.com/questions/5159882/how-to-check-for-a-json-response-using-rspec) – infused Jul 03 '14 at 20:22

1 Answers1

1

As per the @infused link, there will be problem to you to parse the 'response.body' because it is a string. You can do it like this

response.body.should eq('false')

or

response.body.should eq('true')

But I would like to suggest that render json with {key => value} pair.

cweston
  • 11,297
  • 19
  • 82
  • 107
Sanket
  • 490
  • 4
  • 11