Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7513041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:03:22+00:00 2026-05-30T00:03:22+00:00

I need to know how to test this controller action def create_mobile if mobile_user

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:03:23+00:00Added an answer on May 30, 2026 at 12:03 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to know, in a context of a unit test, if Jetbrains IntelliJ
Need to know this so that i could send DTMF and that is going
I have this controller: Ext.define('MyApp.controller.Test', { extend: 'Ext.app.Controller', config: { }, refs: [ {
I need to know about Epoll On linux System. Could you recommend manual or
I need to know how the performance of different XML tools (parsers, validators, XPath
I need to know how much space occupies all the databases inside an SQL
I need to know how to turn on Code Coverage when running TFS builds
I need to know when the memory will be allocated for a particular program.
I need to know, from within Powershell, if the current drive is a mapped
I need to know how I get the stream of the headers and footers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.