20. 事例3: Globalな値を上書きしているテスト
RSpec.describe "new feature", type: :request do
context "on production env" do
before { Rails.env = "production" }
it "not displays" do
get "/new_feature"
expect(response).to have_http_status(:not_found)
end
end
end
production になってしまうと、他のテストで意図しないエラーが
起こる可能性がある
21. 事例3: 修正方法
RSpec.describe "new feature", type: :request do
context "on production env" do
before { allow(Rails.env).to receive(:production?) { true } }
it "not displays" do
get "/new_feature"
expect(response).to have_http_status(:not_found)
end
end
end
代わりにstubを使う。stubは別のテストに影響しない
28. Capybara の基本的な動き
RSpec.feature "xxx", type: :feature do
after { DatabaseRewinder.clean }
scenario do
visit "/"
click_link "hello"
expect(page).to have_content "Hello"
end
end