Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test a file upload in rails for integration testing?

I do some integration testing like this :

 def user.excel_import
          fixture_excel = fixture_file_upload('goodsins.xls', 'text/xls')
          post excel_import_goods_ins_goods_ins_path, :dump=> {:excel_file=>fixture_excel}, :html => { :multipart => "true" }
          assert_response :redirect
          assert_redirected_to goods_ins_path
       end

But when I run the testing it is said that : goodsins.xls file does not exist. FYI : I put the file in the folder that named fixtures.

Any idea? Thx u very much

like image 236
Michelle Avatar asked Jun 11 '12 10:06

Michelle


People also ask

What are integration tests in rails?

Integration tests are used to test how various parts of our application interact. They are generally used to test important workflows within our application. For creating Rails integration tests, we use the test/integration directory for our application. Rails provides a generator to create an integration test skeleton for us.

How do I import data from CSV to TestRail?

User can simply map CSV columns to TestRail’s test case fields. TestRail also supports importing separate test steps and expected results if user uses multiple rows for a single test case.

How do I run a single test file in rails?

Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. $ bin/rails test test/models/article_test.rb Run options: --seed 1559 # Running: .. Finished in 0.027034s, 73.9810 runs/s, 110.9715 assertions/s. 2 runs, 3 assertions, 0 failures, 0 errors, 0 skips

What is test_should_get_index in rails?

Let's take a look at one such test, test_should_get_index from the file articles_controller_test.rb. In the test_should_get_index test, Rails simulates a request on the action called index, making sure the request was successful and also ensuring that the right response body has been generated.


1 Answers

The notes here: http://apidock.com/rails/ActionController/TestProcess/fixture_file_upload indicate that you need to include a slash before the path or file name.

try fixture_file_upload('/goodsins.xls', 'text/xls') and see if that helps.

fixture_file_upload Source:

# File actionpack/lib/action_controller/test_process.rb, line 523
def fixture_file_upload(path, mime_type = nil, binary = false)
  if ActionController::TestCase.respond_to?(:fixture_path)
    fixture_path = ActionController::TestCase.send(:fixture_path)
  end

  ActionController::TestUploadedFile.new("#{fixture_path}#{path}",
    mime_type, binary)
end

Update from Question Owner:

Solution:

add include ActionDispatch::TestProcess to test_helper.rb

like image 107
Matenia Rossides Avatar answered Sep 18 '22 22:09

Matenia Rossides