Reputation: 12837
I need to know how to test this controller action
def create_mobile
if mobile_user = MobileUser.authenticate(params[:email], params[:password])
session[:mobile_user_id] = mobile_user.id
respond_to do |format|
format.json { head :ok }
end
else
respond_to do |format|
format.json { head :unauthorised }
end
end
end
The route is a post request to sessions/create_mobile and as you can see the action only responds to json
My current controller spec looks like this
describe SessionsController, "Logging in" do
before(:each) do
@mobile_user = FactoryGirl.create(:valid_mobile_user)
end
it "Should log in mobile user" do
@request.env["HTTP_ACCEPT"] = "application/json"
post :create_mobile, {:password => @mobile_user.password, :email => @mobile_user.email}
response.should be_success
end
it "should fail log in mobile user" do
@request.env["HTTP_ACCEPT"] = "application/json"
post :create_mobile, {:password => 'joe', :email => @mobile_user.email}
response.should_not be_success
end
end
The test results in
1) SessionsController Logging in should log in mobile user
Failure/Error: response.should be_success
expected success? to return true, got false
# ./spec/controllers/sessions_controller_spec.rb:11:in `block (2 levels) in <top (required)>'
So there is either a problem with my application code or there is a problem with my test code but either way there is no problem with the authenticate method as the MobileUser model spec passes which looks like this
it "should authenticate" do
mu = FactoryGirl.create(:valid_mobile_user)
assert_equal 1, MobileUser.count
assert_equal mu, MobileUser.authenticate(mu.email, mu.password)
end
Any help in sorting this out greatly appreciated. Thanks in advance
UPDATE As suggested below, using
post :create_mobile, {:password => 'joe', :email => @mobile_user.email} :format => :json
or using
@request.env["HTTP_ACCEPT"] = "application/json"
or a combination of both makes no difference
UPDATE 2 The test has just started working for no reason that I can fathom (other than I never understood why it wasn't working in the first place). Totally strange!
Upvotes: 3
Views: 2777
Reputation: 106
I met the same issue recently. below is my test code
post :create, :login => 'mock_user', :password => 'passwd', :format => :json
expected = {
:login => 'mock_user'
}.to_json
session[:user_id].should == mock_user.id
response.body.should == expected
The test result is false, caused by response.body is blank. I checked the test.log, and found got 406 unacceptable request error in controller. After googled a lot, I changed to :format => 'json'. Then I got the expected test result.
Upvotes: 9
Reputation: 15056
Since you're trying to post JSON to the controller, shouldn't you convert your hash to JSON?
post :create_mobile, {:password => 'joe', :email => @mobile_user.email}.to_json
If you don't want to add seven characters to your code just to see if it works, then you should output your params to logger to see what they look like.
def create_mobile
logger.info "LOGIN PARAMS U/N: #{params[:email]}, P/W: #{params[:password]}"
...
Then tail -f log/test.log to see what your controller looks like during your test.
Upvotes: 1