【问题标题】:how to impements gem shoulda in unit testing rails 4 without rspec?如何在没有 rspec 的情况下在单元测试 rails 4 中阻止 gem shoulda?
【发布时间】:2014-09-03 05:01:16
【问题描述】:

如何在没有规范的单元测试中实现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

【问题讨论】:

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


    【解决方案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
    ....
    

    希望这会有所帮助 - 皮埃尔

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 2023-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      相关资源
      最近更新 更多