Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an rspec test that validates a JSON response?

I have a Groups Controller with a method def inbox.

If the user is a group member then inbox returns a JSON object.

If the user is not a member, then inbox should redirect thanks to CanCan permissions.

How do I write an rspec to test these two use cases?

Current spec:

require 'spec_helper'

describe GroupsController do
  include Devise::TestHelpers

  before (:each) do
    @user1 = Factory.create(:user)
    @user1.confirm!
    sign_in @user1
    @group = Factory(:group)
    @permission_user_1 = Factory.create(:permission, :user => @user1, :creator_id => @user1.id, :group => @group)
  end

  describe "GET inbox" do
    it "should be successful" do
      get inbox_group_path(@group.id), :format => :json
      response.should be_success
    end
  end
end

Routes:

inbox_group GET /groups/:id/inbox(.:format) {:controller=>"groups", :action=>"inbox"}

Routes File:

resources :groups do
  member do
    get 'vcard', 'inbox'
  end
  ....
end
like image 729
AnApprentice Avatar asked May 13 '11 16:05

AnApprentice


People also ask

How do I get JSON response data?

GET JSON dataawait fetch('/api/names') starts a GET request, and evaluates to the response object when the request is complete. Then, from the server response, you can parse the JSON into a plain JavaScript object using await response. json() (note: response.

What is RSpec testing?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.

What is JSON in API testing?

JSON stands for JavaScript Object Notation. JSON is a human and machine-readable format to represent data as Structured Data. JSON is used primarily to transfer data from one computer to another or even between different programs on the same computer.

How do I set up RSpec?

Installing RSpecBoot up your terminal and punch in gem install rspec to install RSpec. Once that's done, you can verify your version of RSpec with rspec --version , which will output the current version of each of the packaged gems. Take a minute also to hit rspec --help and look through the various options available.


1 Answers

This is how I do this:

describe "GET index" do
  it "returns correct JSON" do
    # @groups.should have(2).items
    get :index, :format => :json
    response.should be_success
    body = JSON.parse(response.body)
    body.should include('group')
    groups = body['group']
    groups.should have(2).items
    groups.all? {|group| group.key?('customers_count')}.should be_true
    groups.any? {|group| group.key?('customer_ids')}.should be_false
  end
end

I'm not using cancan, therefore I cannot help with this part.

like image 194
Roman Avatar answered Sep 23 '22 08:09

Roman