Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何为多个';应具有#u选择器';_Ruby On Rails_Rspec2_Rspec Rails - Fatal编程技术网

Ruby on rails 如何为多个';应具有#u选择器';

Ruby on rails 如何为多个';应具有#u选择器';,ruby-on-rails,rspec2,rspec-rails,Ruby On Rails,Rspec2,Rspec Rails,正如您在阅读标题时所猜测的,我正在使用Rspec测试我的RoR代码。在我的测试中,我想验证在某个操作之后我是否在某个页面上,并且还要验证该页面上是否存在某些元素。为此,我使用以下代码: # Show page title it { should have_selector('title', text: "Button text") } # Show form fields it { should have_selector('input', id: "user_email") } it { s

正如您在阅读标题时所猜测的,我正在使用Rspec测试我的RoR代码。在我的测试中,我想验证在某个操作之后我是否在某个页面上,并且还要验证该页面上是否存在某些元素。为此,我使用以下代码:

# Show page title
it { should have_selector('title', text: "Button text") }

# Show form fields
it { should have_selector('input', id: "user_email") }
it { should have_selector('input', id: "user_password") }
it { should have_selector('input', id: "user_password_confirmation") }
it { should have_selector('input', value: "Schrijf me in") }
但在另一个测试中,我也想验证是否在同一页上。我可以再次使用上面的代码,但感觉不对


我想使用Rspec自定义匹配器来完成此操作。但我能找到的唯一示例需要一个参数传递给匹配器,或者只能验证一个选择器。如何编写自定义匹配器来执行此操作?

如果您的测试相同,您需要的是

shared_examples 'on user home page' do
  # Show page title
  it { should have_selector('title', text: "Button text") }

  # Show form fields
  it { should have_selector('input', id: "user_email") }
  it { should have_selector('input', id: "user_password") }
  it { should have_selector('input', id: "user_password_confirmation") }
  it { should have_selector('input', value: "Schrijf me in") }
end

# Spec File #1
describe 'Test one' do
  it_behaves_like 'on user home page'
end

# Spec File #2
describe 'Test two' do
  it_behaves_like 'on user home page'
end