I'm writing an integration test for rspec and I want to te开发者_如何转开发st that clicking on a link goes to the correct model page. More specifically, I want to test this on a list of links that have the same name but different underlying URLs.
From what I've learned so far, you cannot use a css attribute selector with click_link as it only looks for specific text or dom ID. Instead I'm trying to use the webrat within
method, but once I select the link how do I click it? I figured link.click
within the link's scope would work, but it fails saying the method click
is undefined:
Failure/Error: link.click
NoMethodError:
undefined method `click' for #<Webrat::Scope:0x0000010505ae00>
Here's my test:
require 'spec_helper'
describe "BrandLinks" do
before(:each) do
@base_title = "My App - "
@brand = Factory(:brand)
second = Factory(:brand) # <= Same name, different slug
third = Factory(:brand, :name => "Awesome USA Brand!!")
@brands = [@brand, second, third]
end
it "should show me the brand page when I click on a brand link" do
get '/brands'
within "a[href=#{brand_path(@brand)}]" do |link|
link.click
end
response.should be_success
response.should have_selector(
"title",
:content => "#{@base_title}Brand - #{@brand.name}"
)
end
end
Have you tried click_link_within? According to the docs
Works like click_link, but only looks for the link text within a given selector
First, the error message is that the .click
method is undefined. Try .click_link
instead.
You'll also need the text of the link. That is, within
defines a scope, and click_link
tells it which link to click inside that scope.
within "a[href=#{brand_path(@brand)}]" do |scope| # within a specific <a> tag
scope.click_link @brand # click on @brand text
end
click_link_within
is a shortcut for within plus click_link, so this should be identical to the above:
click_link_within "a[href=#{brand_path(@brand)}]", @brand
I'm still trying to understand how the within
selectors work myself (which is how I found this question!). Here's what I've got so far:
The Webrat Session object's within
method takes a selector
argument, which it uses to push a new scope onto a stack by passing the selector to Scope.from_scope, which creates a new session with @selector = selector
. So far I can't find the definition of selector
. This 2009 blog post says a selector can be a CSS type, class, or ID, but it doesn't cite a source for that.
精彩评论