berkes
berkes

Reputation: 27553

Define correct `fixture_path` for cucumber

I want to attach a file, from a cucumber step:

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

For some reason this results in /home/xxx/test/fixtures/green_small.jpg. Somewhere the default for fixture_path is test/fixtures in cucumber.

I am using rspec, so the path to the fixtures should be spec/fixtures. My Rspec spec_helper.rb has this configured:

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
end

I have already tried setting this in config/environments/test.rb without success.

Obviously, I can simply build the path myself, page.attach_file("Image", File.join(Rails.root, "spec", "fixtures", "green_small.jpg")) works. But that fixture_path is already there. Setting that correct and then using it, would make the steps just a little more portable and cleaner.

But this is not used by Cucumber. How do I get the correct url in cucumber?

Upvotes: 4

Views: 2425

Answers (3)

Tilo
Tilo

Reputation: 33732

Just as a note, the reason why you can't put this into your environments/*.rb files is that this config block is for RSpec.config ... not for YourAppName::Application.configure

You have to modify the path in your ./spec/spec_helper.rb file.

Upvotes: 0

Billy Chan
Billy Chan

Reputation: 24815

How about you define Cucumber global variables in World which is a rb file in features/support

File features/support/path_define.rb

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

World(PathDefine)

Then you can use my_fixture_path variable anywhere in step definitions.

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

Hope this helps.

Upvotes: 1

Rodrigo Oliveira
Rodrigo Oliveira

Reputation: 913

According to this post, you should use the path in the test, not a environment

Like this:

attach_file(:csv_file, File.join(RAILS_ROOT, 'features', 'upload-files', 'products_csv_ok.csv'))

Upvotes: 0

Related Questions