事例3: Globalな値を上書きしているテスト
RSpec.describe "newfeature", 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 "newfeature", 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は別のテストに影響しない
事例4: 修正方法
module StubConstAutoLoader
defstub_const(constant_name, value, options = {})
constant_name.deconstantize.safe_constantize
super
end
end
RSpec::Mocks::ExampleMethods.prepend StubConstAutoLoader
Capybara の基本的な動き
RSpec.feature "xxx",type: :feature do
after { DatabaseRewinder.clean }
scenario do
visit "/"
click_link "hello"
expect(page).to have_content "Hello"
end
end