Search code examples
rspeccucumberfixtures

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?


Solution

  • 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.