2010-07-23 63 views

回答

1

尝试是这样的(我没有试过运行,所以可能需要一些小改动):

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 
+0

未定义的方法'find_link'for#(NoMethodError) – 2010-07-28 11:25:06

0

已经有预定义的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 
+0

能否请您写在这里的链路中的一个字一个例子,我尝试没有成功: 我应该能看到“富”中的“A” 我应该看到在“富”“[ a]“ 我应该在”[a]“ 之内看到”[foo]“,但它不起作用。 – 2010-07-24 10:32:31

0

你可能有更多的运气使用xpaths为此,你可以使用类似“// a/text()='foo'”。你只需要为webrat启用xpaths,我写了一篇博客文章,关于在这里使用xpaths做些类似于http://www.opsb.co.uk/?p=165的表,它包括启用webrat的xpaths所需的代码。

0
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