lundi 27 juillet 2015

Unit Testing for Uploading files using CarrierWave with fixtures

I am trying to make unit tests pass for a Comit controller, however, it seems like CarrierWave is adding an additional step that I think I am missing. I am using CarrierWave to upload files and mounting it in a column of my comits table called file_url.

In my comits_controller_test.rb, both my should_create_comit and should_update_comit tests are failing, and I have no clue where to go next. And I am pretty sure it's because of my fixtures for comits.yml.

Focusing in should_update_comit, I am getting this error: ArgumentError: Can't detect the type of /share/home/rruizvev /dev/chemtrack/public/uploads/comit/file_url/980190962 /File.open(Rails.root.join("test/fixtures/files/20150702129999.xls")) - ease use the :extension option to declare its type.

I added a folder under fixtures/folder and added the .xls there, however, the update test is looking for it in the public/uploads/.... path instead of the path that I gave it in the fixture! I know I am probably explaining this in a really confusing way, but I would appreciate any help.

Here is the create and update method from my comits_controller

  # POST /comits.json
  def create
    @comit = Comit.new(comit_params)
    @comit.user = current_user
  # Saves the file with Comit tag into the file_app_name coloum
   @comit.file_app_name = @comit.filename +  @comit.file_url.filename



    respond_to do |format|
      if @comit.save
        format.html { redirect_to comits_url, notice: 'Comit was successfully created.' }
        format.json { render :show, status: :created, location: @comit }
      else
        format.html { render :new }
        format.json { render json: @comit.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /comits/1
  # PATCH/PUT /comits/1.json
  def update
    respond_to do |format|
      if @comit.update(comit_params)
        format.html { redirect_to @comit, notice: 'Comit was successfully updated.' }
        format.json { render :show, status: :ok, location: @comit }
      else
        format.html { render :edit }
        format.json { render json: @comit.errors, status: :unprocessable_entity }
      end
    end
  end

And here is my comits_contoller_test.rb

require 'test_helper'

class ComitsControllerTest < ActionController::TestCase
  setup do
    @comit = comits(:one)
    @update = {
        filename: 'MyString',
        file_url: 'File.open(Rails.root.join("test/fixtures/file/20150702129999.xls"))',
        file_kilobytes: 1,
        file_record_count: 1,
        added_by_user_id: 1,
        file_app_name:'MyString'
    }
  end

  test "should get index" do
    login
    get :index
    assert_response :success
    assert_not_nil assigns(:comits)
  end

  test "should get new" do
    login
    get :new
    assert_response :success
  end

  test "should create comit" do
    login
    assert_difference('Comit.count') do
      post :create, @update
    end

    assert_redirected_to comit_path(assigns(:comit).user)
  end

  test "should show comit" do
    login
    get :show, id: @comit
    assert_response :success
  end

  test "should get edit" do
    login
    get :edit, id: @comit
    assert_response :success
  end

  test "should update comit" do
    login
    patch :update, id: @comit, comit: @update
    assert_redirected_to comit_path(assigns(:comit))
  end

  test "should destroy comit" do
    login
    assert_difference('Comit.count', -1) do
      delete :destroy, id: @comit
    end

    assert_redirected_to comits_path
  end
end

And my comit.yml fixtures.

one:
  filename: MyString
  file_url: File.open(Rails.root.join("test/fixtures/file /20150702129999.xls"))
  file_kilobytes: 1
  file_record_count: 1
  added_by_user_id: 1
  file_app_name: MyString

Aucun commentaire:

Enregistrer un commentaire