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

  • SEARCH
  • Home
  • 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 8350401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:18:23+00:00 2026-06-09T08:18:23+00:00

I followed the RailsCast #350 REST API Versioning and #352 Securing an API which

  • 0

I followed the RailsCast #350 REST API Versioning and #352 Securing an API which creates an API under app/controllers/api/v1 so you can type in localhost:3000/api/v1/restaurants and you’ll get the JSON for restaurants index.

I want to be able to do some functional testing. I’ve been trying to figure this out but am not sure how to do this.

Here’s what I’ve done so far:

I created api_v1_restaurants_controller_test.rb in my test/functional folder and did something like this:

require 'test_helper'
include Devise::TestHelpers

class ApiV1RestaurantsControllerTest < ActionController::TestCase
  fixtures :users, :roles, :restaurants, :menus, :ingredients, :dishes, :drinks

  setup do
    @restaurant = restaurants(:applebees)
    @user = users(:admin)
    @api = api_keys(:one)
  end

  test "should get restaurant index json data" do   
    assert_routing(
        'api/v1/restaurants',
        {:controller => 'api/v1/restaurants', :action => 'index', :format => 'json', :api_key => @api},
        {},
        {:api_key => @api}
    )
  end

The should get restaurant index json data test seems to work but I want to be able to test to see whether api/v1/restaurants/1?api_key=123456789 generates JSON, which it should.

Here what I’ve tried to write up:

test "should get restaurant id json data" do
  get 'api/v1/restaurants', :format => :json, :api_key => @api
  # get :get, :format => :json, :api_key => @api

  json = JSON.parse(@response.body)

  assert_equal Restaurant.first.id, json.first["id"]
end

But I get the following error on my console after running rake test:functionals:

test_should_get_restaurant_id_json_data(ApiV1RestaurantsControllerTest):
RuntimeError: @controller is nil: make sure you set it in your test's setup method.

Update 1: Defined @controller

So I listened to the error message and defined @controller under my setup do... as @controller = Api::V1 but now get the following error message on the console:

test_should_get_restaurant_id_json_data(ApiV1RestaurantsControllerTest):
NoMethodError: undefined method `response_body=' for Api::V1:Module
  • 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-06-09T08:18:27+00:00Added an answer on June 9, 2026 at 8:18 am

    I was able to finally figure it all out.

    You need to define your @controller like this:

    setup do
      ...
      @api = api_keys(:one)
      @restaurant = restaurants(:applebees)
      @controller = Api::V1::RestaurantsController.new
    end
    

    And then you should be able to create a test like this to test your JSON:

    test "json should be valid" do
      get :show, :format => :json, :api_key => @api.access_token, :id => @restaurant.id
      json = JSON.parse(@response.body)
    
      assert_equal @restaurant.id, json["id"]
      ...
    end
    

    If you ever need to see what JSON is being produced, you can simply write (inside your test block):

    puts json # This will output the entire JSON to your console from JSON.parse(@response.body)
    

    I hope this helps other people as well.

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

Sidebar

Related Questions

I just followed railscast 134 to install paperclip into my rails 3.0.1 app. I
Followed instructions on Railscast but can't get server to start. It states the following
I followed the tutorial Create a model through text field (Railscast #57) which works
I followed the railscast that describes how to get workling running background tasks, but
I followed the directions in Railscast #17 HABTM Checkboxes (revised) to get this code
I followed the Railscast to get Sunspot running and then this tutorial on enabling
I have followed the following railscast about adding pdfkit to an application, and I
I'm trying to create a multi-level ajax form on my rails app that can
I've followed a railscast on creating and displaying static pages using a pages model
I've just followed the Railscast tutorial: http://railscasts.com/episodes/262-trees-with-ancestry Is it possible to paginate results from

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.