我想這個自定義步驟:我如何測試某個詞是否與黃瓜有關聯?
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