请求测试
render_template:
expect(response).to render_template("new")
redirect_to
expect(response).to redirect_to()
请求测试,功能测试:
have_http_status (:error/:missing/:redirect/:success)
在功能测试:
expect(page).to have_http_status(:success)
在请求测试:
expect(response).to have_http_status(201)
expect(response).not_to have_http_status(:created)
Model Specs描述模型的行为,一般基于数据库。
Request Spec
主要是:给了一个request,HTTP的响应response。 也可以叫integration。
请求测试一般在spec/requests, spec/api, spec/integration目录,标志:type => :require
请求测试混入了Rails集成测试的模块Runner中的行为。
很多开发者喜欢FactoryBot/Capybara一起用在请求测试。
require 'rails_helper'
RSpec.describe "home page", :type => :request do
it "displays the user's username after successful login" do
user = FactoryBot.create(:user, :username => "jdoe", :password => "secret")
visit "/login"
fill_in "Username", :with => "jdoe"
fill_in "Password", :with => "secret"
"Log in"
expect(page).to have_selector(".header .username", :text => "jdoe")
end
end
因此没有使用get "/login"这种HTTP verb加路径url的定位.
Feature Specs从外部模仿浏览器。如果使用Capybara,请求测试和功能测试,集成测试都差不多。