gpt4 book ai didi

ruby-on-rails - 在 Capybara 中使用 Given/Let w/Feature/Scenario

转载 作者:数据小太阳 更新时间:2023-10-29 08:20:32 24 4
gpt4 key购买 nike

我想使用“给定”(或“让”)设置一个变量,我的 spec.rb 文件中的所有“功能”都可以访问该变量。我该怎么做呢? “给定”语句应该位于文件中的什么位置?谢谢!

require 'spec_helper'

feature "Home page" do
given(:base_title) { "What Key Am I In?" }
scenario "should have the content 'What Key Am I In?'" do
visit '/static_pages/home'
expect(page).to have_content('What Key Am I In?')
end

scenario "should have the title 'What Key Am I In? | Home'" do
visit '/static_pages/home'
expect(page).to have_title("#{base_title}")
end

scenario "should not have a custom page title | Home'" do
visit '/static_pages/home'
expect(page).not_to have_title("| Home")
end
end

feature "About page" do
scenario "should have the content 'About'" do
visit '/static_pages/about'
expect(page).to have_content('About')
end

scenario "should have the title 'What Key Am I In? | About'" do
visit '/static_pages/about'
expect(page).to have_title('What Key Am I In? | About')
end
end

最佳答案

given/let 调用在 feature/describe/context block 的顶部使用,并应用于所有包含的 feature/describe/contextscenario/it block 。在您的情况下,如果您有两个单独的 feature block ,您希望将它们包含在更高级别的 feature/describe/context block 中并放置任何 given/let 您想要应用到更高级别的所有调用。

引用在 RSpec 中使用的 capybara 文档:

feature is in fact just an alias for describe ..., :type =>
:feature
, background is an alias for before, scenario for it, and given/given! aliases for let/let!, respectively.

此外,在 RSpec 中,describe block (无论是通过 describecontext 还是 Capybara 别名 feature 表示) 可以任意深度嵌套。相反,在 Cucumber 中,feature 只能存在于规范的顶层。

您可以谷歌“rspec nested describe”获取更多信息。

关于ruby-on-rails - 在 Capybara 中使用 Given/Let w/Feature/Scenario,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17864449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com