Ruby on rails Rails API rspec测试
我正在构建一个API,Ruby on rails Rails API rspec测试,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我正在构建一个API,用户可以跟随并被其他用户跟随。我创建了一个关系表来处理与关系端点的关联。以下是我跟踪某人的终点: 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)
用户
可以跟随并被其他用户
跟随。我创建了一个关系
表来处理与关系
端点的关联。以下是我跟踪某人的终点:
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
一切都按计划进行。如您所见,我想用我的当前用户
对象和新创建的关系
对象进行响应。当我点击这个端点时,这两种方法都会起作用,它提供了一个授权令牌和我想要跟踪的用户的id。我遇到的问题是测试我是否返回了关系
对象。以下是我对以下端点的测试:
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 = {: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\u 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\u response[1])时,我收到了一个错误。这是一种(关系)
1)Api::V1::RelationshipController POST#成功创建关系时创建应与创建的关系对象一起响应
失败/错误:期望(用户响应[1])是(关系)的类型
期望[{:id=>153,:follower\u id=>1239,:follower\u id=>1240,:created\u at=>“2016-03-28818:18:43.064Z”,:updated\u at=>“2016-03-28818:18:43.064Z}]成为一种关系(id:integer,follower\u id:integer,followerd\u id:integer,created\u at:datetime,updated\u:datetime)
#./spec/controllers/api/v1/relationship_controller_spec.rb:27:in“block(4级)in”
关于如何测试返回的第二个对象是关系类中的对象,有什么建议吗?另外,如果有一种更“正确”的方法来处理这个端点,我将非常感谢这一课=) “create”方法返回的是JSON响应,而不是ActiveRecord对象。HTTP响应不能是“类型(关系)”。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作为请求中的参数发送
好的,这是有道理的。我只是将其更改为检查
follower\u id
与@user
id。关于不在API中使用会话。您共享的链接也在其API演示中使用会话。这也是我在成功登录时返回auth_令牌的方法。我引用的其他示例/教程都通过返回身份验证令牌的“无状态”会话处理用户登录。有更好的方法来处理这个问题吗?我喜欢将函数所需的所有数据作为JSON对象发送,因为这种方法更显式,在文档中更容易设置:{token_number:'3453xf343',token_secret:'*****',user_id:56,follower_id:36}注意“https”协议。用户在所有调用中发送toek和token_secret。
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