我有一个带有def inbox.方法的组控制器
如果用户是组成员,则inbox返回一个JSON对象。
如果用户不是成员,则由于CanCan权限,收件箱应该重定向。
如何编写rspec来测试这两个用例?
当前规格:
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路由:
inbox_group GET /groups/:id/inbox(.:format) {:controller=>"groups", :action=>"inbox"}路由文件:
resources :groups do
  member do
    get 'vcard', 'inbox'
  end
  ....
end发布于 2011-05-14 03:18:46
我是这样做的:
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我没有使用cancan,所以这部分我帮不上忙。
发布于 2013-11-27 23:11:04
有时,验证response是否包含有效的JSON并显示实际响应可能已经足够好了,下面是一个示例:
it 'responds with JSON' do
  expect {
    JSON.parse(response.body)
  }.to_not raise_error, response.body
end发布于 2011-10-17 18:39:53
试试这个:
_expected = {:order => order.details}.to_json
response.body.should == _expectedhttps://stackoverflow.com/questions/5995170
复制相似问题