2012-12-06 170 views
3

正確`fixture_path`我想附加文件,從黃瓜步:定義黃瓜

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

我正在使用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已經在那裏。設置正確,然後使用它,將使這些步驟變得更輕便更清潔。

但是這不被黃瓜使用。我如何在黃瓜中獲得正確的網址?

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

回答

1

你怎麼樣在World這是一個RB文件中定義黃瓜全局變量:

0

this後,你應該在測試中,沒有一個環境

像這樣使用路徑features/support

文件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

希望這有助於。

+0

好吧,顯然我可以在步驟中設置自己的路徑(我將更新問題以反映這一點)。事情是,我有一個名爲「fixtures_path」的輔助變量(方法?),它已經包含了一個目錄的路徑。只有它是錯誤的道路。設置正確,然後使用它,會使步驟更清潔。 – berkes

+1

您可以在spec_helper.rb中創建一個名爲「fixtures_path」的方法並返回正確的路徑。我認爲這樣可以正常工作。 –

0

正如一個說明,爲什麼你不能把這個變成你的環境的原因/ *。RB文件是這個配置塊是RSpec.config ......不是YourAppName::Application.configure

你必須修改路徑在你的./spec/spec_helper.rb文件中。