我想在rspec中测试一下是否存在提交按钮。我也在用水豚。
我试过了:
should have_tag("input","Submit button")
和
should have_content("Submit, button")
但它要么会引发异常,要么会给出误报。
发布于 2013-03-18 01:11:03
这些都是很好的建议,但是如果你想确认它是一个按钮,并且它有正确的值(用于显示),你必须更详细一些:
page.should have_selector("input[type=submit][value='Press Me']")
我不知道现有的匹配器可以做到这一点。这是我写的一个自定义RSpec2匹配器:
RSpec::Matchers.define :have_submit_button do |value|
match do |actual|
actual.should have_selector("input[type=submit][value='#{value}']")
end
end
这是zwippie版本( RSpec3 @zwippie):
RSpec::Matchers.define :have_submit_button do |value|
match do |actual|
expect(actual).to have_selector("input[type=submit][value='#{value}']")
end
end
我将其与其他自定义匹配器一起保存在spec/support/matchers/request_matchers.rb
中。RSpec会自动拾取它。由于这是一个RSpec匹配器(而不是Capybara查找器),它将在功能规格(Capybara)和视图规格(没有Capybara的RSpec)中工作。
功能规格用法:
page.should have_submit_button("Save Me")
查看规范使用情况(调用render
之后):
rendered.should have_submit_button("Save Me")
请注意,如果您在Capybara请求规范中,并且想要与提交按钮进行交互,这会容易得多:
click_button "Save Me"
不能保证它实际上是一个提交按钮,但你的功能规范应该只是测试行为,而不是担心细节级别。
发布于 2015-02-17 17:29:11
有一个内置的matcher has_button?。
使用RSpec,您可以获得如下断言
page.should have_button('Submit button')
或者使用新的RSpec 3语法:
expect(page).to have_button('Submit button')
发布于 2013-12-31 04:57:17
我有一个(用于黄瓜):
Then /^I should see "([^"]*)" button/ do |name|
should have_button name
end
否定用法: have_no_button
https://stackoverflow.com/questions/12573518
复制相似问题