Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Minitest Controller :create action with Paperclip Validators

Basically my :create action test keeps failing, even though it works in the app. I commented out the paperclip validations in my controller below and it worked.

  has_attached_file :image, styles: { medium: "700x700>", small: "350x250#" }
  validates_attachment_presence :image
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/

This is the test I am running, it works when the validations are commented out. How can i pass params that will meet the paperclip validations in my model.

test "should create if signed_in" do
      sign_in(@user)
      assert_difference 'Product.count' do
        post :create, product:{ title:'test_product', description: 'khara', user_id: @user.id}
      end
      assert_redirected_to product_path(assigns(:product))
    end

FAILURE MESSAGE:

    FAIL["test_should_post_create_if_signed_in", ProductsControllerTest, 0.58458]
         test_should_post_create_if_signed_in#ProductsControllerTest (0.58s)
                "Product.count" didn't change by 1.
                Expected: 3
                  Actual: 2
                test/controllers/products_controller_test.rb:52:in `block in <class:ProductsControllerTest>'

Basically how do I make this test pass?

NOTE: I am aware that paperclip provides testing instructions with Shoulda and Spec was hoping to do this purely in Minitest.

like image 945
ChiefRockaChris Avatar asked Jun 10 '15 06:06

ChiefRockaChris


People also ask

What is a custom validator?

Custom Validation Creating a custom validator entails rolling out our own annotation and using it in our model to enforce the validation rules. So let's create our custom validator, which checks phone numbers.

How do I configure Minitest as a dependency via bundler?

Let’s start a new Ruby project where we’ll configure Minitest as a dependency via Bundler by adding it to our Gemfile. Create a new directory, and put the following code in your Gemfile: Next, open your project’s directory in terminal, and run bundle install to install the latest version of Minitest in your bundle.

When to use custom validation in Spring MVC?

Overview Generally, when we need to validate user input, Spring MVC offers standard predefined validators. However, when we need to validate a more particular type of input, we have the ability to create our own custom validation logic.

How do Minitest tests work?

As you can see, Minitest is so simple that its tests are run just like any other Ruby program. The output above shows the report from the tests that were run, including information about test duration and errors. Our test case failed because the MagicBall class does not exist.


1 Answers

You should just attach an issue using ActionDispatch::TestProcess, fixture_file_upload. Put an image you want to use for your tests in test/fixtures adjust your test to look like this:

test "should create if signed_in" do
          sign_in(@user)
          image = fixture_file_upload('some_product_image.jpg', 'image/jpg')
          assert_difference 'Product.count' do
            post :create, product:{ title:'test_product', 
                               description: 'khara', 
                                   user_id: @user.id,
                                     image: image
                                   }
          end
          assert_redirected_to product_path(assigns(:product))
        end

This will return an object that pretends to be an uploaded file for paperclip.

like image 50
KTHero Avatar answered Nov 14 '22 21:11

KTHero