lundi 20 février 2017

Testing allowed file types and file size - Carrierwave

This is my factory

FactoryGirl.define do
  factory :post do
    image   { File.open(File.join(Rails.root, 'spec/files/hello-world.png')) }
    caption { FFaker::Lorem.sentence }
  end
end

And this is the test I have on models/post_spec.rb for validations

require 'rails_helper'

RSpec.feature Post, :type => :model do

  let(:post) { build(:post) }

  describe 'validation rules' do
    it { expect(post).to validate_presence_of(:image) }
    it { expect(post).to validate_presence_of(:caption) }
  end
end

How can I do tests for validating the allowed file types and the file size? Does these tests need to placed in the post model or is it better to write features tests in this case?

Edit Example (on pseudo code - using Capybara & RSpec)

require 'rails_helper'

RSpec.feature 'Creating posts', :type => :feature do
  scenario 'cannot create post without image' do
    # visit root
    # click 'New Post'

    # upload file 'spec/files/hello-world.txt'
    # click submit button

    # expect to see validation message with invalid filetype  
  end
end

Would this test do the work properly ?

Btw I'm using Carrierwave for the file uploading.

Aucun commentaire:

Enregistrer un commentaire