【问题标题】:Rails API rspec testsRails API rspec 测试
【发布时间】:2016-03-29 03:06:11
【问题描述】:

我正在构建一个 API,Users 可以跟随并被其他 Users 跟随。我创建了一个Relationship 表来处理关联以及Relationship 端点。这是我关注某人的终点:

def create
 user = User.find(params[:followed_id])
 if user
   current_user.follow(user)
   render json: [current_user, Relationship.where(followed_id: user.id, follower_id: current_user.id)], status: 201, location: [:api, current_user]
 else
   render json: { errors: current_user.errors }, status: 422
 end
end

一切都按预期工作。如您所见,我想用我的current_user 对象和新创建的Relationship 对象来响应。当我点击此端点提供授权令牌和我想要关注的用户的 id 时,这两种方法都有效。我遇到的问题是测试我得到了一个Relationship 对象。以下是我对以下端点的测试:

describe "POST #create" do

 context "When relationship is successfully created" do
   before(:each) do
     @user = FactoryGirl.create(:user)
     other_user = FactoryGirl.create(:user)
     api_authorization_header(@user.auth_token)
     post :create, followed_id: other_user.id
   end

   it "should respond w/ current_user object" do
     user_response = JSON.parse(response.body, symbolize_names: true)
     expect(user_response[0][:id]).to eq(@user.id)
   end

### THIS TEST IS FAILING & THE ONE THAT I NEED HELP WITH.
   it "should respond w/ created relationship object" do
     user_response = JSON.parse(response.body, symbolize_names: true)
     expect(user_response[1]).to be_kind_of(Relationship)
   end

   it { should respond_with(201) }
 end

end

这是user_response的一些输出:

user_response = {:id=>1233, :email=>"desmond.pollich@stanton.co", :first_name=>"Marcelina", :last_name=>"Morissette", :created_at=>"2016-03-28T18:16:09.875Z", :updated_at=>"2016-03-28T18:16:09.875Z", :auth_token=>"Ky25sYoJc4gH-p122yEH"}
{:id=>150, :follower_id=>1233, :followed_id=>1234, :created_at=>"2016-03-28T18:16:09.892Z", :updated_at=>"2016-03-28T18:16:09.892Z"}

你可以看到user_response 正在返回我要求它响应的两个对象的数组。

user_response[1] = {:id=>150, :follower_id=>1233, :followed_id=>1234, :created_at=>"2016-03-28T18:16:09.892Z", :updated_at=>"2016-03-28T18:16:09.892Z"}

这是我在尝试运行 `expect(user_response[1]).to be_kind_of(Relationship) 时收到的错误

1) Api::V1::RelationshipController POST #create When relationship is successfully created should respond w/ created relationship object
 Failure/Error: expect(user_response[1]).to be_kind_of(Relationship)
   expected [{:id=>153, :follower_id=>1239, :followed_id=>1240, :created_at=>"2016-03-28T18:18:43.064Z", :updated_at=>"2016-03-28T18:18:43.064Z"}] to be a kind of Relationship(id: integer, follower_id: integer, followed_id: integer, created_at: datetime, updated_at: datetime)
 # ./spec/controllers/api/v1/relationship_controller_spec.rb:27:in `block (4 levels) in <top (required)>'

关于如何测试被发回的第二个对象是来自关系类的对象的任何建议?另外,如果有一种更“正确”的方式来处理这个端点,我会很感激这个教训=)。

【问题讨论】:

    标签: ruby-on-rails ruby rspec


    【解决方案1】:

    “create”方法返回的是 JSON 响应,而不是 ActiveRecord 对象。 HTTP 响应不能是“be_kind_of(Relationship)”。 JSON 只是格式化的文本。

    如果您要构建 RESTful API,您必须测试请求中的 JSON 响应和 HTTP 状态,而不是测试控制器。比如:

    it 'updates a user on update_user action' do
      params = {
        id:     @user.id,  # FactoryGirl object
        fname: 'John',
        lname: 'Smith',
        email: 'newemail@ert.com'
      }
    
      post '/v1/users/update', params, @env
    
      # test for the 200 status-code
      expect(response).to be_success
      expect(json['data']['email']).to eq 'newemail@ert.com'
    end   
    

    顺便说一句,您不应该在 API 中使用会话。而是在您的请求中将您的用户 ID 作为参数发送。

    http://matthewlehner.net/rails-api-testing-guidelines/

    【讨论】:

    • 好吧,这很有道理。我只是将其更改为检查 follower_id@user id。
    • 关于不在您的 API 中使用会话。您共享的链接还在其 API 演示中使用了会话。这也是我在成功登录时返回auth_token 的方法。我引用的其他示例/教程都通过返回身份验证令牌的“无状态”会话处理用户登录。有没有更好的方法来处理这个问题?
    • 我喜欢将函数所需的所有数据作为 JSON 对象发送,因为这种方式更明确且易于在文档中设置:api.yourcompany.com/v1/crateuser{token_number:'3453xf343', token_secret:'* ****', user_id: 56, follower_id: 36} 注意“https”协议。用户在所有调用中发送 toek 和 token_secret。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 2014-08-03
    相关资源
    最近更新 更多