加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails – 使用功能测试测试JSON API

发布时间:2020-12-17 04:31:13 所属栏目:百科 来源:网络整理
导读:我只有一个简单的问题,但我找不到任何答案. 我的ruby on rails 3.2.2 appilcation有一个带有设计会话认证的JSON API. 我的问题是:我如何通过功能测试或集成测试来测试这个API – 有没有办法处理会话? 我没有前端,只有我可以做的API GET. POST.放.并使用JSO
我只有一个简单的问题,但我找不到任何答案.

我的ruby on rails 3.2.2 appilcation有一个带有设计会话认证的JSON API.

我的问题是:我如何通过功能测试或集成测试来测试这个API – 有没有办法处理会话?

我没有前端,只有我可以做的API GET. POST.放.并使用JSON Body进行删除.

哪种测试方法是自动化的最佳方法?

示例创建新用户

发布www.exmaple.com/users

{
 "user":{
    "email" : "test@example.com","password " : "mypass"
  }
}

解决方法

功能测试很容易.在用户示例中,我将它们放在Rspec中的spec / controllers / users_controller_spec.rb中:
require 'spec_helper'

 describe UsersController do
   render_views # if you have RABL views

   before do
     @user_attributes = { email: "test@example.com",password: "mypass" }
   end

   describe "POST to create" do

     it "should change the number of users" do
        lambda do
          post :create,user: @user_attributes
        end.should change(User,:count).by(1)
     end

     it "should be successful" do
       post :create,user: @user_attributes
       response.should be_success
     end

     it "should set @user" do
       post :create,user: @user_attributes
       assigns(:user).email.should == @user_attributes[:email]
     end

     it "should return created user in json" do # depend on what you return in action
       post :create,user: @user_attributes
       body = JSON.parse(response.body)
       body["email"].should == @user_attributes[:email]
      end
  end

显然,你可以优化上面的规格,但这应该让你开始.干杯.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读