Search code examples
ruby-on-railsunit-testingshoulda

how to impements gem shoulda in unit testing rails 4 without rspec?


how to implements gem shoulda in unit testing without respec?, this is my gemfile file :

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.4'

group :production do
    # Use PostgreSQL as the database for Active Record on Heroku
    gem 'pg'
end

group :development, :test do
    # Use sqlite3 as the database for Active Record
    gem 'sqlite3'
    gem "minitest"
    gem 'byebug'
end

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer',  platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0',          group: :doc

# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring',        group: :development

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]
# gem 'byebug', group: [:development, :test]

gem 'devise'
gem 'shoulda'
gem 'omniauth-facebook'
gem 'omniauth-google-oauth2'

and this is my test_helper.rb file :

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
include Devise::TestHelpers
require 'minitest/autorun'
require 'shoulda/rails'

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

this is my example model testing unit :

require 'test_helper'

class UserTest < ActiveSupport::TestCase
  should validate_presence_of(:name)
  should validate_presence_of(:last_name)
  should validate_presence_of(:id_number)
  should ensure_inclusion_of(:id_issuing_country).in_array(["Spain"])
  should validate_presence_of(:birth_date)
  should validate_presence_of(:email)
  should validate_presence_of(:bank_account)
end

when i runing this in my console : bin/rake test test/models/user_test.rb

i get error like this :

LoadError: cannot load such file -- shoulda/rails

Solution

  • From a gem file for application I am working on

    ....
    group :test do
      # gem 'faker'
      gem 'launchy'
      gem 'relish' 
      gem 'selenium-webdriver',  '~> 2.42.0'
      gem 'capybara'
      gem 'database_cleaner'
      gem 'shoulda-matchers', '~>2.6.2'
      gem 'shoulda'
    end
    ....
    

    hope this helps - Pierre