如何在不使用rspec的单元测试导轨4中使用gem should?

时间:2014-09-03 05:01:16

标签: ruby-on-rails unit-testing shoulda

如何在没有respec的单元测试中实现gem shoulda,这是我的gemfile文件:

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'

这是我的test_helper.rb文件:

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

这是我的示例模型测试单元:

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

当我在我的控制台中运行时:bin/rake test test/models/user_test.rb

我得到这样的错误:

LoadError: cannot load such file -- shoulda/rails

1 个答案:

答案 0 :(得分:1)

从我正在处理的应用程序的gem文件中

....
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
....

希望这有帮助 - 皮埃尔