水豚内部方法的正确用法是什么? [英] What is the correct usage of capybara's within method?
问题描述
对于页脚的功能示例,我使用以下代码:
For feature examples for the footer I've used this code:
feature 'in footer' do
scenario "has a Copyright text" do
within('footer') {
expect(page).to have_content "Copyright"
}
end
scenario "has navigation bar" do
within('footer') {
expect(page).to have_selector 'nav ul li'
}
end
scenario "has a link for 'About'" do
within('footer') {
expect(page).to have_link 'About', href: '#'
}
end
end
如果您仔细观察,我在每个场景,这与代码干涩相冲突。
If you look closely I repeated the "within" in each scenario and this conflicts with dryness of code.
我不想在一个场景中包含所有期望,因为我想对每个期望都有一个解释。
I do not want to include all expectation in one scenario because I want an explanation for each of them.
在这种情况下使用内部方法的最佳方法是什么?
What is the best method for using the within method in this situation?
推荐答案
没有办法使使用方法变干。 #within并且仍然有多种情况。有人可能会尝试使用周围过滤器,但由于周围过滤器和前后过滤器的顺序不同,因此它实际上无法正常工作。无需使用#within即可获得所需的内容,方法是在before块中找到页脚,然后进行预期
There is no way to dry up the use of #within and still have multiple scenarios. Someone might try to use an around filter, but because of the ordering of around and before/after filters it's not really going to work. You can get what you're looking for without using #within, by finding the footer in a before block and then expect off that
before do
visit('my page')
@footer = find('footer')
end
scenario 'blah blah' do
expect(@footer).to have_content('...')
end
I会说编写功能测试仅检查页面上的一行文本并不是一个好习惯。功能测试有很多开销,并且检查不依赖于任何用户操作的一行文本确实更适合于视图测试而不是功能(您仍然可以在视图测试中使用Capybaras匹配器)。功能测试应用于测试系统中的较大行为。
I will say that writing feature tests only to check for a line of text on the page is not great practice. Feature tests have a lot of overhead, and checking for a line of text thats not dependent on any user actions is really more suited to a view test rather than feature (You can still use Capybaras matchers in view tests). Feature tests should be for testing larger behaviors in the system.
这篇关于水豚内部方法的正确用法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!