如何创建验证 JSON 响应的 rspec 测试?

发布于 2024-11-07 06:28:18 字数 943 浏览 0 评论 0原文

我有一个组控制器,其方法为 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

阿楠 2024-11-14 06:28:18

我就是这样做的:

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,因此我无法帮助完成这部分。

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.

柒夜笙歌凉 2024-11-14 06:28:18

有时,验证 response 是否包含有效的 JSON 并显示实际响应可能就足够了,下面是一个示例:

it 'responds with JSON' do
  expect {
    JSON.parse(response.body)
  }.to_not raise_error, response.body
end

Sometimes it might be good enough to verify if response contains valid JSON and to show actual response otherwise, here is an example:

it 'responds with JSON' do
  expect {
    JSON.parse(response.body)
  }.to_not raise_error, response.body
end
冷弦 2024-11-14 06:28:18

试试这个:

_expected = {:order => order.details}.to_json
response.body.should == _expected

Try this:

_expected = {:order => order.details}.to_json
response.body.should == _expected
可可 2024-11-14 06:28:18

我认为您要做的第一件事是检查响应的类型是否正确,即它的 Content-Type 标头设置为 application/json, 然后,根据您的情况

it 'returns JSON' do
  expect(response.content_type).to eq(Mime::JSON)
end

,您可能需要检查响应是否可以解析为 JSON,例如 wik< /a> 建议:

it 'responds with JSON' do
  expect {
    JSON.parse(response.body)
  }.to_not raise_error
end

如果您觉得检查 JSON 响应有效性的两个测试太多,您可以将上述两个合并为一个测试。

I think the first thing you want to do is to check that the response is of the correct type, i.e. that it has the Content-Type header set to application/json, something along the lines of:

it 'returns JSON' do
  expect(response.content_type).to eq(Mime::JSON)
end

Then, depending on your case, you might want to check whether the response can be parsed as JSON, like wik suggested:

it 'responds with JSON' do
  expect {
    JSON.parse(response.body)
  }.to_not raise_error
end

And you could merge the above two into a single test if you feel like two tests for checking JSON response validity are too much.

叹沉浮 2024-11-14 06:28:18

要断言 JSON,您也可以这样做:

ActiveSupport::JSON.decode(response.body).should == ActiveSupport::JSON.decode(
  {"error" => " An email address is required "}.to_json
)

此博客提供了更多想法。

To assert JSON you can do this too:

ActiveSupport::JSON.decode(response.body).should == ActiveSupport::JSON.decode(
  {"error" => " An email address is required "}.to_json
)

This blog gives some more ideas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文