我想这个自定义步骤:我如何测试某个词是否与黄瓜有关联?
Then I should see the link 'foo'
和他相反:
But I should not see the link 'foo'
在页面上我能有这样的事情:
lorem foo bar
或可替代
lorem <a href=''>foo</a> bar
我需要测试何时“富”是一个链接,什么时候不是。谢谢。
我想这个自定义步骤:我如何测试某个词是否与黄瓜有关联?
Then I should see the link 'foo'
和他相反:
But I should not see the link 'foo'
在页面上我能有这样的事情:
lorem foo bar
或可替代
lorem <a href=''>foo</a> bar
我需要测试何时“富”是一个链接,什么时候不是。谢谢。
尝试是这样的(我没有试过运行,所以可能需要一些小改动):
Then /^I should see the link "([^\"]*)"$/ do |linked_text|
# AFAIK this raises an exception if the link is not found
find_link(linked_text)
end
Then /^I should not see the link "([^\"]*)"$/ do |linked_text|
begin
find_link(linked_text)
raise "Oh no, #{linked_text} is a link!"
rescue
# cool, the text is not a link
end
end
已经有预定义的Webrat
这样做的步骤(或者至少非常相似)。从web_steps.rb
Then /^(?:|I)should see \/([^\/]*)\/ within "([^\"]*)"$/ do |regexp, selector|
within(selector) do |content|
regexp = Regexp.new(regexp)
if defined?(Spec::Rails::Matchers)
content.should contain(regexp)
else
assert_match(regexp, content)
end
end
end
和
Then /^(?:|I)should not see "([^\"]*)" within "([^\"]*)"$/ do |text, selector|
within(selector) do |content|
if defined?(Spec::Rails::Matchers)
content.should_not contain(text)
else
hc = Webrat::Matchers::HasContent.new(text)
assert !hc.matches?(content), hc.negative_failure_message
end
end
end
能否请您写在这里的链路中的一个字一个例子,我尝试没有成功: 我应该能看到“富”中的“A” 我应该看到在“富”“[ a]“ 我应该在”[a]“ 之内看到”[foo]“,但它不起作用。 – 2010-07-24 10:32:31
你可能有更多的运气使用xpaths为此,你可以使用类似“// a/text()='foo'”。你只需要为webrat启用xpaths,我写了一篇博客文章,关于在这里使用xpaths做些类似于http://www.opsb.co.uk/?p=165的表,它包括启用webrat的xpaths所需的代码。
Then /^I should not see the link "([^\"]*)"$/ do |linked_text|
page.should_not have_css("a", :text => linked_text)
end
Then /^I should see the link "([^\"]*)"$/ do |linked_text|
page.should have_css("a", :text => linked_text)
end
未定义的方法'find_link'for#(NoMethodError) –
2010-07-28 11:25:06