在我的应用中,我在两个单独的页面上有相同的表单。我已经为一个页面编写了一系列测试,并希望简单地遍历我的第二页的测试套件,传入测试套件应该运行的页面。问题是,我似乎无法以干净的方式访问url_helpers
。
到目前为止,这是我的工作:
require 'rails_helper'
describe "my tests" do
subject { page }
paths = ["/signin", "/signup"]
paths.each do |path|
describe "user signs in via #{path}" do
before { visit path }
describe "user enters invalid information" do
before do
fill_in "Email", with: user.email, match: :first
fill_in "Password", with: user.password.reverse, match: :first
click_button "Sign in"
end
it { should have_title("Sign in") }
it { should have_content "Invalid email/password combination!" }
its(:current_path) { should eq "/signin" }
end
end
end
如果我将"/signin"
替换为signin_path
,我会
undefined local variable or method `signin_path' for RSpec::ExampleGroups::Authentication:Class (NameError)
但是我在我的测试脚本的其余部分成功使用了url_helpers,虽然在之前,它阻止了。如果我使用Rails.application.routes.url_helpers.signin_path
完全限定了url_helper,那么它可以正常工作,但我宁愿自动完成这项工作。如果我在脚本的顶部做了一个包含,那么我得到
ActionView::Template::Error:
arguments passed to url_for can't be handled. Please require routes or provide your own implementation
再次,不理想。我宁愿做这件事。有什么想法吗?
提前致谢。
答案 0 :(得分:0)
如果你只是做了一条路,是否没有错误?如果是这样,那么为什么不使用RSpec shared_example
require 'rails_helper'
shared_examples 'get_user_in' do |path|
describe "user signs in via #{path}" do
before { visit path }
describe "user enters invalid information" do
before do
fill_in "Email", with: user.email, match: :first
fill_in "Password", with: user.password.reverse, match: :first
click_button "Sign in"
end
it { should have_title("Sign in") }
it { should have_content "Invalid email/password combination!" }
its(:current_path) { should eq "/signin" }
end
end
end
describe "/signin" do
subject { page }
include_examples "get_user_in", 'signin'
end
describe "/signup" do
subject { page }
include_examples "get_user_in", 'signup'
end
没有测试过,所以这里可能会有一些错误。