Я пишу интеграционный тест для rspec и хочу проверить, что нажатие на ссылку ведет на правильную страницу модели. В частности, я хочу проверить это на списке ссылок с одинаковым именем, но разными базовыми URL-адресами.
Из того, что я узнал до сих пор, вы не можете использовать селектор атрибутов css с click_link, поскольку он ищет только определенный текст или идентификатор dom. Вместо этого я пытаюсь использовать метод webrat within, но как только я выберу ссылку, как мне ее щелкнуть? Я подумал, что link.click в рамках ссылки будет работать, но он не говорит, что метод click не определен:
Failure/Error: link.click
NoMethodError:
undefined method `click' for #<Webrat::Scope:0x0000010505ae00>
Вот мой тест:
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
[When] code is easy to test, it's often a sign that the architecture is coherent and decoupled. I have definitely changed/refactored code purely for testability. I would certainly be more wary of doing this if it actually made the code more complex, but adding an id to an html element, for example, doesn't really increase your technical debt. If it makes the code more testable, I'd say go for it. If the element is important enough for you to test that it's there, it's probably fine for you to give it an id or class.Очко принято. - person Chris Bloom   schedule 25.05.2011