如何在RSpec中执行以下操作?
test "should get home" do
get :home
assert_response :success
get :home, { mobile: 1 }
assert_response :success
end
请注意,我的移动视图具有不同的mime类型(即.mobile.erb)
尝试失败:
render_views
describe "GET home" do
it "renders the index view" do
get :home
expect(response).to render_template("home")
get :home, { mobile: 1 }
expect(response).to render_template("home")
end
end
如果我打破移动视图,此测试不会失败。
答案 0 :(得分:1)
要检查请求是否成功,您可以使用:
it { is_expected.to respond_with 200 }
最佳做法是每次测试single expectation,我会将您的示例重构为以下内容:
describe "GET home" do
render_views
context 'with regular view' do
before do
get :home
end
it { is_expected.to respond_with 200 }
it "renders the index view" do
expect(page).to have_content 'some header'
end
end
context 'with mobile view' do
before do
get :home, { mobile: 1 }
end
it { is_expected.to respond_with 200 }
it "renders the index view" do
expect(page).to have_content 'some header'
end
end
end
这只是你开始的想法。
答案 1 :(得分:0)
你可以试试这个:
expect(response).to be_success