gpt4 book ai didi

rspec - 为 cucumber 定义正确的 `fixture_path`

转载 作者:行者123 更新时间:2023-12-02 03:52:51 24 4
gpt4 key购买 nike

我想从 cucumber 步骤附加一个文件:

When /^I attach an image/ do
page.attach_file("Image", File.join(fixture_path, "green_small.jpg"))
end

出于某种原因,这会导致 /home/xxx/test/fixtures/green_small.jpg .某处fixture_path 的默认值是 test/fixtures在 cucumber 中。

我正在使用 rspec,所以固定装置的路径应该是 spec/fixtures .我的 Rspec spec_helper.rb有这个配置:
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
end

我已经尝试在 config/environments/test.rb 中设置它没有成功。

显然,我可以简单地自己构建路径, page.attach_file("Image", File.join(Rails.root, "spec", "fixtures", "green_small.jpg"))作品。但是那个 fixture_path已经在那里了。正确设置然后使用它,将使这些步骤更加便携和清洁。

但这不是 Cucumber 使用的。如何在 cucumber 中获得正确的网址?

最佳答案

你如何在 World 中定义 Cucumber 全局变量这是 features/support 中的 rb 文件

文件 features/support/path_define.rb

module PathDefine
def my_fixture_path
@my_fixture_path ||= "#{::Rails.root}/spec/fixtures"
end
end

World(PathDefine)

那么你可以使用 my_fixture_path步骤定义中任何位置的变量。

引用: https://github.com/cucumber/cucumber/wiki/A-Whole-New-World

希望这可以帮助。

关于rspec - 为 cucumber 定义正确的 `fixture_path`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13748800/

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