The Paperclip Documentation provides instruction for setting up Paperclip's Shoulda matchers for RSpec and Test::Unit. However when I tried to set them up for Minitest I wasn't successful (I followed the same instructions as for Test::Unit).
Does anyone knows what requires to make Papercips Shoulda matchers work with Minitest?
In case anyone comes here from Google like I did...this ended up working for me with minitest-spec-rails & some custom assertions
# test_helper.rb
require 'paperclip/matchers'
class ActiveSupport::TestCase
extend Paperclip::Shoulda::Matchers
include Paperclip::Shoulda::Matchers
# ... other code
end
module Minitest
module Assertions
##
# Passes if matcher.matches?(subject) returns true
#
# Example:
#
# def test_validations
# assert_must be_valid, #user
# end
def assert_must(matcher, subject, msg = nil)
msg = message(msg) do
if matcher.respond_to? :failure_message
matcher.failure_message # RSpec 3.x, 1.1
else
matcher.failure_message_for_should # RSpec 2.x, 1.2
end
end
assert matcher.matches?(subject), msg
end
##
# Facilitator to assert_must for use with minitest-spec. If no subject
# given, defaults to matching against the current `subject` or the
# instance variable `#subject`.
#
# Example:
#
# subject { Order.new }
#
# it "should have associations" do
# must belong_to :account
# must have_many :line_items
# end
def must(matcher, subject = #subject || subject, msg = nil)
assert_must matcher, subject, msg
end
##
# Passes if matcher.matches?(subject) returns false
#
# Example:
#
# def test_validations
# assert_wont be_valid, #user
# end
def assert_wont(matcher, subject, msg = nil)
msg = message(msg) do
if matcher.respond_to? :failure_message_when_negated # RSpec 3.x
matcher.failure_message_when_negated # RSpec 3.x
elsif matcher.respond_to? :failure_message_for_should_not
matcher.failure_message_for_should_not # RSpec 2.x, 1.2
else
matcher.negative_failure_message # RSpec 1.1
end
end
if matcher.respond_to? :does_not_match?
assert matcher.does_not_match?(subject), msg
else
refute matcher.matches?(subject), msg
end
end
##
# Facilitator to assert_wont for use with minitest-spec. If no subject
# given, defaults to matching against the current `subject` or the
# instance variable `#subject`.
#
# Example:
#
# subject { User.new }
#
# it "should validate" do
# wont have_valid(:email).when("foo", "foo#bar", "#bar.com")
# end
def wont(matcher, subject = #subject || subject, msg = nil)
assert_wont matcher, subject, msg
end
end
end
I've created a gem for this: https://github.com/cschramm/rspec2minitest
Just include it on your Gemfile and put require 'rspec2minitest/paperclip' in your test_helper.
Related
I am using request_confirmation_test.rb file from devise
confirmation test from devise
I have included the helper too but the file is showing errors on each line. Looks like something is missing that is necessary for the file to work correctly.This is the error that I am getting currently
ConfirmationInstructionsTest#test_set_up_sender_from_custom_mailer_defaults:
NoMethodError: undefined method `from' for nil:NilClass
test/integration/request_confirmation_test.rb:46:in `block in <class:ConfirmationInstructionsTest>'
this is my code for testing file
require 'test_helper'
class ConfirmationInstructionsTest < ActionMailer::TestCase
def setup
setup_mailer
Devise.mailer = 'Devise::Mailer'
Devise.mailer_sender = 'johnmax.rtc#gmail.com'
end
def teardown
Devise.mailer = 'Devise::Mailer'
Devise.mailer_sender = 'please-change-me#config-initializers-devise.com'
end
def user
#user ||= create_user
end
def mail
#mail ||= begin
user
ActionMailer::Base.deliveries.first
end
end
test 'email sent after creating the user' do
assert_not_nil mail
end
# test 'content type should be set to html' do
# assert mail.content_type.include?('text/html')
# end
test 'send confirmation instructions to the user email' do
mail
assert_equal [user.email], mail.to
end
test 'set up sender from configuration' do
assert_equal ['johnmax.rtc#gmail.com'], mail.from
end
test 'set up sender from custom mailer defaults' do
Devise.mailer = 'Users::Mailer'
assert_equal ['johnmax.rtc#gmail.com'], mail.from
end
# test 'set up sender from custom mailer defaults with proc' do
# Devise.mailer = 'Users::FromProcMailer'
# assert_equal ['johnmax.rtc#gmail.com'], mail.from
# end
# test 'custom mailer renders parent mailer template' do
# Devise.mailer = 'Users::Mailer'
# assert_present mail.body.encoded
# end
# test 'set up reply to as copy from sender' do
# assert_equal ['johnmax.rtc#gmail.com'], mail.reply_to
# end
# test 'set up reply to as different if set in defaults' do
# Devise.mailer = 'Users::ReplyToMailer'
# assert_equal ['johnmax.rtc#gmail.com'], mail.from
# assert_equal ['johnmax.rtc#gmail.com'], mail.reply_to
# end
test 'set up subject from I18n' do
store_translations :en, devise: { mailer: { confirmation_instructions: { subject: 'Account Confirmation' } } } do
assert_equal 'Account Confirmation', mail.subject
end
end
test 'subject namespaced by model' do
store_translations :en, devise: { mailer: { confirmation_instructions: { user_subject: 'User Account Confirmation' } } } do
assert_equal 'User Account Confirmation', mail.subject
end
end
test 'body should have user info' do
assert_match user.email, mail.body.encoded
end
test 'body should have link to confirm the account' do
host, port = ActionMailer::Base.default_url_options.values_at :host, :port
if mail.body.encoded =~ %r{<a href=\"http://#{host}:#{port}/users/confirmation\?confirmation_token=([^"]+)">}
assert_equal $1, user.confirmation_token
else
flunk "expected confirmation url regex to match"
end
end
# test 'renders a scoped if scoped_views is set to true' do
# swap Devise, scoped_views: true do
# assert_equal user.email, mail.body.decoded
# end
# end
# test 'renders a scoped if scoped_views is set in the mailer class' do
# begin
# Devise::Mailer.scoped_views = true
# assert_equal user.email, mail.body.decoded
# ensure
# Devise::Mailer.send :remove_instance_variable, :#scoped_views
# end
# end
test 'mailer sender accepts a proc' do
swap Devise, mailer_sender: proc { "another#example.com" } do
assert_equal ['another#example.com'], mail.from
end
end
end
This is the helper file
require 'active_support/test_case'
class ActiveSupport::TestCase
VALID_AUTHENTICATION_TOKEN = 'AbCdEfGhIjKlMnOpQrSt'.freeze
def setup_mailer
ActionMailer::Base.deliveries = []
end
def store_translations(locale, translations, &block)
# Calling 'available_locales' before storing the translations to ensure
# that the I18n backend will be initialized before we store our custom
# translations, so they will always override the translations for the
# YML file.
I18n.available_locales
I18n.backend.store_translations(locale, translations)
yield
ensure
I18n.reload!
end
def generate_unique_email
##email_count ||= 0
##email_count += 1
"test#{##email_count}#example.com"
end
def valid_attributes(attributes={})
{ first_name: "usertest",
email: generate_unique_email,
password: '12345678',
password_confirmation: '12345678' }.update(attributes)
end
def new_user(attributes={})
User.new(valid_attributes(attributes))
end
def create_user(attributes={})
User.create!(valid_attributes(attributes))
end
def create_admin(attributes={})
valid_attributes = valid_attributes(attributes)
valid_attributes.delete(:first_name)
Admin.create!(valid_attributes)
end
def create_user_without_email(attributes={})
UserWithoutEmail.create!(valid_attributes(attributes))
end
def create_user_with_validations(attributes={})
UserWithValidations.create!(valid_attributes(attributes))
end
# Execute the block setting the given values and restoring old values after
# the block is executed.
def swap(object, new_values)
old_values = {}
new_values.each do |key, value|
old_values[key] = object.send key
object.send :"#{key}=", value
end
clear_cached_variables(new_values)
yield
ensure
clear_cached_variables(new_values)
old_values.each do |key, value|
object.send :"#{key}=", value
end
end
def clear_cached_variables(options)
if options.key?(:case_insensitive_keys) || options.key?(:strip_whitespace_keys)
Devise.mappings.each do |_, mapping|
mapping.to.instance_variable_set(:#devise_parameter_filter, nil)
end
end
end
end
Any help would be highly appreciated.
I added cancancan gem to my app, but it broked all my controller and feature tests. The error start in ability.rb initialize: the user variable is nil.
Here is the error log of one of the tests:
14) TestCasesController DELETE #destroy when not logged-in redirects to sign-in page
Failure/Error: if user.teacher?
NoMethodError:
undefined method `teacher?' for nil:NilClass
# ./app/models/ability.rb:5:in `initialize'
# /usr/local/rvm/gems/ruby-2.3.1/gems/cancancan-1.15.0/lib/cancan/controller_additions.rb:361:in `new'
# /usr/local/rvm/gems/ruby-2.3.1/gems/cancancan-1.15.0/lib/cancan/controller_additions.rb:361:in `current_ability'
# /usr/local/rvm/gems/ruby-2.3.1/gems/cancancan-1.15.0/lib/cancan/controller_additions.rb:342:in `authorize!'
# /usr/local/rvm/gems/ruby-2.3.1/gems/cancancan-1.15.0/lib/cancan/controller_resource.rb:49:in `authorize_resource'
# /usr/local/rvm/gems/ruby-2.3.1/gems/cancancan-1.15.0/lib/cancan/controller_resource.rb:34:in `load_and_authorize_resource'
# /usr/local/rvm/gems/ruby-2.3.1/gems/cancancan-1.15.0/lib/cancan/controller_resource.rb:10:in `block in add_before_action'
# /usr/local/rvm/gems/ruby-2.3.1/gems/rails-controller-testing-1.0.1/lib/rails/controller/testing/template_assertions.rb:61:in `process'
# /usr/local/rvm/gems/ruby-2.3.1/gems/devise-4.2.0/lib/devise/test/controller_helpers.rb:33:in `block in process'
# /usr/local/rvm/gems/ruby-2.3.1/gems/devise-4.2.0/lib/devise/test/controller_helpers.rb:100:in `catch'
# /usr/local/rvm/gems/ruby-2.3.1/gems/devise-4.2.0/lib/devise/test/controller_helpers.rb:100:in `_catch_warden'
# /usr/local/rvm/gems/ruby-2.3.1/gems/devise-4.2.0/lib/devise/test/controller_helpers.rb:33:in `process'
# /usr/local/rvm/gems/ruby-2.3.1/gems/rails-controller-testing-1.0.1/lib/rails/controller/testing/integration.rb:12:in `block (2 levels) in <module:Integration>'
# ./spec/controllers/test_cases_controller_spec.rb:182:in `block (3 levels) in <top (required)>'
# ./spec/controllers/test_cases_controller_spec.rb:193:in `block (4 levels) in <top (required)>'
Here is the corresponding action:
describe "DELETE #destroy" do
let!(:test_case) { create(:test_case) }
subject { delete :destroy, params: { id: test_case } }
context "when not logged-in" do
before { subject }
it "redirects to sign-in page" do
expect(response).to redirect_to(new_user_session_url)
end
end
end
This is my controller:
class TestCasesController < ApplicationController
include ApplicationHelper
load_and_authorize_resource
before_action :authenticate_user!
before_action :find_test_case, only: [:show, :edit, :update, :destroy, :test]
before_action :find_question, only: [:index, :new, :create, :test_all]
def index
#test_cases = TestCase.where(question: #question)
end
def new
#test_case = TestCase.new
end
def create
#test_case = #question.test_cases.build(test_case_params)
if #test_case.save
flash[:success] = "Caso de teste criado!"
redirect_to #test_case
else
render 'new'
end
end
def show
end
def edit
end
def update
if #test_case.update_attributes(test_case_params)
flash[:success] = "Caso de teste atualizado!"
redirect_to #test_case
else
render 'edit'
end
end
def destroy
question = #test_case.question
#test_case.destroy
flash[:success] = "Caso de teste deletado!"
redirect_to question_test_cases_url(question)
end
def test
result = #test_case.test(plain_current_datetime, "pas", params[:source_code])
#output = result[:output]
#results = [ { title: #test_case.title, status: result[:status] } ]
respond_to { |format| format.js }
end
def test_all
#results = #question.test_all(plain_current_datetime, "pas", params[:source_code])
respond_to { |format| format.js }
end
private
def test_case_params
params.require(:test_case).permit(:title, :description, :input, :output, :question_id)
end
def find_test_case
#test_case = TestCase.find(params[:id])
end
def find_question
#question = Question.find(params[:question_id])
end
end
This is my ability class:
class Ability
include CanCan::Ability
def initialize(user)
if user.teacher?
can :manage, [Exercise, Question, TestCase]
can :manage, Team, owner: user
can [:read, :create], Team
can [:enroll], Team do |team|
!team.enrolled?(user)
end
can [:unenroll], Team do |team|
team.enrolled?(user)
end
end
end
end
This is my rails_helper (from rspec):
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
require 'support/helpers/relationships'
# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, :type => :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
# Helpers
config.include Helpers::Relationships, type: :model
config.include Devise::Test::ControllerHelpers, :type => :controller
config.include Warden::Test::Helpers
end
Any tips of how to fix that?
OBS: I'm using Devise for authentication, it provides me the required current_user to cancancan work.
The primary reason you're getting a nil:NilClass error is because current_user is nil unless you are signed in via Devise. Your ability needs to handle for when a user is not logged in, something like this:
def initialize(user)
return unless user.present?
if user.teacher?
...
end
end
If you return nil from your ability, the authorization fails and is handled accordingly.
I am just starting with RSpec and I am a bit lost. Can anyone please tell me how to write controller test for this method?
def list_c
#c = params.has_key?(:car) ? Car.new(car_listing_params) : Car.new()
#car.user = #user
#car.number_of_beds = 4 unless #car.number_of_beds
#car.car_type_tag = 'car' unless #car.car_type
#page_title = t('home.list_your_car')
end
A small example of controller test with rspec. You may get some idea from here.
# define your helper here.
require 'rails_helper'
# respect method to define test for your controller.
RSpec.describe YourController, type: :controller do
#define your factories for the set of your test
# you will decide which one you need to create from factory. Factory girl
# is used to mock factories.
# #c = params.has_key?(:car) ? Car.new(car_listing_params) : Car.new()
# #car.user = #user
# #car.number_of_beds = 4 unless #car.number_of_beds
# #car.car_type_tag = 'car' unless #car.car_type
# #page_title = t('home.list_your_car')
let(:user) { FactoryGirl.create :user }
before { sign_in user }
describe '#title' do
context 'with valid params' do
before do
# in before block, you can request a http call
# with parameter. This action repeats before each test runs.
get :your_action_of_current_controller, {a_param: a_value, a_date: "16-4-2015"}
end
it "renders successfully" do
# when you call a get request it assigns a response object.
expect(response).to be_success
end
it "returns json" do
expect(response.content_type).to be == 'application/json'
end
end
end
end
I am using paperclip-dropbox gem to store my assets. I want to create a test to ensure assets are properly destroyed if the destroy checkbox is checked.
I can see on dropbox the image is uploaded and deleted but just after it's created again. Don't understand why.
Here is my test file:
require 'test_helper'
#
# == Admin namespace
#
module Admin
#
# == SettingsController test
#
class SettingsControllerTest < ActionController::TestCase
include Devise::TestHelpers
setup :initialize_test
#
# == Avatar
#
test 'should be able to upload logo' do
upload_dropbox_paperclip_attachment
setting = assigns(:setting)
assert setting.logo?
assert_equal 'bart.png', setting.logo_file_name
assert_equal 'image/png', setting.logo_content_type
end
test 'should be able to destroy logo' do
upload_dropbox_paperclip_attachment
remove_dropbox_paperclip_attachment
end
private
def initialize_test
#setting = settings(:one)
#administrator = users(:bob)
sign_in #administrator
end
def upload_dropbox_paperclip_attachment
puts '=== Uploading logo to Dropbox'
attachment = fixture_file_upload 'images/bart.png', 'image/png'
patch :update, id: #setting, setting: { logo: attachment }
end
def remove_dropbox_paperclip_attachment
puts '=== Removing logo from Dropbox'
patch :update, id: #setting, setting: { logo: nil, delete_logo: '1' }
assert_not assigns(:setting).logo?
end
end
end
Do someone know what is wrong with this test ?
Am I in the right way trying to test for dropbox or should I test only in local ? the documentation for paperclip-dropbox doesn't give any information about testing.
Thanks
My project:
Rails 4.2.3
Ruby 2.2.2
My app has a default controller abstracted into a gem (gem 'abstracted') and all my controllers inherits from this abstracted controller.
# Gemfile
gem 'abstracted', path: '../../gems/abstracted'
# app/controllers/accounts_controller.rb
class AccountsController < AbstractResourcesController
end
Now when I write controller (mini)tests for one of this controllers like this:
require "test_helper"
describe AccountsController do
...
I get this error once hitting: rake test from the prompt:
NameError: uninitialized constant AbstractResourcesController
/path_to_rails/projects/cas_server/app/controllers/accounts_controller.rb:1:in `<top (required)>'
My test/test_helper.rb looks like this:
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'minitest/autorun'
require 'action_controller/test_case'
require 'capybara/rails'
require 'rails/test_help'
require 'minitest-rails'
require 'minitest/pride'
# require 'miniskirt'
# require 'factories'
# require 'mocha'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
class ActionController::TestCase
include Devise::TestHelpers
end
class MiniTest::Unit::TestCase
# include MiniTest::ActiveRecordAssertions
# DatabaseCleaner.strategy = :transaction
#
# def setup
# DatabaseCleaner.start
# end
#
# def teardown
# DatabaseCleaner.clean
# end
end
class MiniTest::Spec
include ActiveSupport::Testing::SetupAndTeardown
# alias :method_name :__name__ if defined? :__name__
def build_message(*args)
args[1].gsub(/\?/, '%s') % args[2..-1]
end
end
class ControllerSpec < MiniTest::Spec
include ActionController::TestCase::Behavior
include Devise::TestHelpers
include Rails.application.routes.url_helpers
# Rails 3.2 determines the controller class by matching class names that end in Test
# This overides the #determine_default_controller_class method to allow you use Controller
# class names in your describe argument
# cf: https://github.com/rawongithub/minitest-rails/blob/gemspec/lib/minitest/rails/controller.rb
def self.determine_default_controller_class(name)
if name.match(/.*(?:^|::)(\w+Controller)/)
$1.safe_constantize
else
super(name)
end
end
before do
#controller = self.class.name.match(/((.*)Controller)/)[1].constantize.new
#routes = Rails.application.routes
end
subject do
#controller
end
end
# Functional tests = describe ***Controller
MiniTest::Spec.register_spec_type( /Controller$/, ControllerSpec )
Add this in your controller spec file before describe AccountsController do,
module AbstractResourcesController
class Base
def self.protect_from_forgery; end
end
end
require 'abstract_resources_controller'