使用 rspec 测试 API 端点
Using rspec to test an API endpoint
我有这个 api 端点无法从我的数据库中获取所有有效的博客 ID 用户通过 api_key。这工作正常,现在我正在尝试测试这个端点。
路线:
Rails.application.routes.draw do
get 'blogs', to: 'blogs#index'
end
博客管理员:
class BlogsController < ApplicationController
def index
if params[:api_key]
user = User.find_by(api_key: params[:api_key])
if user.present?
@blogs = Blog.all
return render json: @blogs, status: :ok
end
end
render json: { error: "Unauthorized!" }, status: :bad_request
end
end
我是 rspec 和一般测试的新手,我看了几个视频和教程,这是我目前所掌握的:
spec/requests/blogs_spec.rb
require 'rails_helper'
RSpec.describe 'Blogs API', type: :request do
let!(:blogs) { Blog.limit(10) }
describe 'GET /blogs' do
before { get '/blogs' }
it 'returns status code 400' do
expect(response).to have_http_status(400)
end
context 'when the request is valid' do
before { get '/blogs', params: { api_key: '123123'} }
it 'returns status code 400' do
expect(response).to have_http_status(200)
end
end
end
end
我似乎无法进行上次测试,我也不知道为什么。我的猜测是我没有正确传递 api_key
,但我不知道如何传递
1) Blogs API GET /blogs when the request is valid returns status code 400
Failure/Error: expect(response).to have_http_status(200)
expected the response to have status code 200 but it was 400
# ./spec/requests/blogs_spec.rb:28:in `block (4 levels) in <top (required)>'
好的,根据你的问题+评论,我可以假设你是 运行 你在 test
环境中进行的测试,但你期望找到 User
存在于 development
数据库.
工厂机器人
您可能想使用 FactoryBot 为您的测试套件创建记录。
添加到您的 Gemfile:
group :development, :test do
gem 'factory_bot_rails'
end
在rails_helper.rb
中添加:
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
现在您应该创建 User
工厂。使用以下内容创建一个新文件 spec/factories/user.rb
:
FactoryBot.define do
factory :user do
api_key { '123123' }
# You should define every any other required attributes here so record can be created
end
end
最后,在您的规范文件中:
....
context 'when the request is valid' do
before { get '/blogs', params: { api_key: user.api_key} }
let!(:user) { create(:user) }
it 'returns status code 200' do
expect(response).to have_http_status(200)
end
end
...
现在你的测试应该通过了。请注意,在测试数据库中也没有创建 Blog
,因此:
let!(:blogs) { Blog.limit(10) }
将return一个空数组。您还需要创建一个 Blog
工厂,并创建如下博客:
let!(:blogs) { create_list(:blog, 2) }
奖金
一旦你开始改进你的测试,你可能想看看 Faker and Database Cleaner for ActiveRecord
我有这个 api 端点无法从我的数据库中获取所有有效的博客 ID 用户通过 api_key。这工作正常,现在我正在尝试测试这个端点。
路线:
Rails.application.routes.draw do
get 'blogs', to: 'blogs#index'
end
博客管理员:
class BlogsController < ApplicationController
def index
if params[:api_key]
user = User.find_by(api_key: params[:api_key])
if user.present?
@blogs = Blog.all
return render json: @blogs, status: :ok
end
end
render json: { error: "Unauthorized!" }, status: :bad_request
end
end
我是 rspec 和一般测试的新手,我看了几个视频和教程,这是我目前所掌握的:
spec/requests/blogs_spec.rb
require 'rails_helper'
RSpec.describe 'Blogs API', type: :request do
let!(:blogs) { Blog.limit(10) }
describe 'GET /blogs' do
before { get '/blogs' }
it 'returns status code 400' do
expect(response).to have_http_status(400)
end
context 'when the request is valid' do
before { get '/blogs', params: { api_key: '123123'} }
it 'returns status code 400' do
expect(response).to have_http_status(200)
end
end
end
end
我似乎无法进行上次测试,我也不知道为什么。我的猜测是我没有正确传递 api_key
,但我不知道如何传递
1) Blogs API GET /blogs when the request is valid returns status code 400
Failure/Error: expect(response).to have_http_status(200)
expected the response to have status code 200 but it was 400
# ./spec/requests/blogs_spec.rb:28:in `block (4 levels) in <top (required)>'
好的,根据你的问题+评论,我可以假设你是 运行 你在 test
环境中进行的测试,但你期望找到 User
存在于 development
数据库.
工厂机器人
您可能想使用 FactoryBot 为您的测试套件创建记录。
添加到您的 Gemfile:
group :development, :test do
gem 'factory_bot_rails'
end
在rails_helper.rb
中添加:
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
现在您应该创建 User
工厂。使用以下内容创建一个新文件 spec/factories/user.rb
:
FactoryBot.define do
factory :user do
api_key { '123123' }
# You should define every any other required attributes here so record can be created
end
end
最后,在您的规范文件中:
....
context 'when the request is valid' do
before { get '/blogs', params: { api_key: user.api_key} }
let!(:user) { create(:user) }
it 'returns status code 200' do
expect(response).to have_http_status(200)
end
end
...
现在你的测试应该通过了。请注意,在测试数据库中也没有创建 Blog
,因此:
let!(:blogs) { Blog.limit(10) }
将return一个空数组。您还需要创建一个 Blog
工厂,并创建如下博客:
let!(:blogs) { create_list(:blog, 2) }
奖金
一旦你开始改进你的测试,你可能想看看 Faker and Database Cleaner for ActiveRecord