标签: shoulda

应该是单元测试中的超类不匹配

尝试使用shoulda和rails 3编写简单的单元测试.

测试/单元/ user_test.rb

class UserTest < Test::Unit::TestCase
  should validate_presence_of(:password, :on => :create)
  should validate_presence_of(:handle, :email)
  should validate_confirmation_of(:password)
  should validate_length_of(:handle, :within => 6..15)
  should validate_uniqueness_of(:handle)
  should validate_format_of(:handle, :with => /\A\w+\z/i)
  should validate_length_of(:email, :within => 6..100)
end
Run Code Online (Sandbox Code Playgroud)

Gemfile的相关部分

group :test do
  gem 'shoulda'
  gem 'rspec-rails', '2.0.0.beta.12'
end
Run Code Online (Sandbox Code Playgroud)

当我尝试使用此命令时,rake test --trace我收到以下错误:

** Execute test:units
/Users/removed/removed/removed/app_name/test/unit/user_test.rb:5: superclass mismatch for class UserTest (TypeError)
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:227:in `load_dependency'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
    from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:9
    from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:9:in `each'
    from …
Run Code Online (Sandbox Code Playgroud)

ruby unit-testing shoulda ruby-on-rails-3

4
推荐指数
1
解决办法
882
查看次数

使用shoulda的NoMethodError validate_presence_of

好吧,我很困惑.我试图在Rails 3.1下使用带有Test :: Unit的shoulda,之前已成功使用Rails 2.3.11.

我的Gemfile中有以下内容:

group :test do
  gem 'shoulda'
end
Run Code Online (Sandbox Code Playgroud)

(我跑了bundle install- bundle show shoulda表演c:/Ruby192/lib/ruby/gems/1.9.1/gems/shoulda-2.11.3)

我有以下test_helper.rb

ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'shoulda'

class ActiveSupport::TestCase
end
Run Code Online (Sandbox Code Playgroud)

以及user_test.rb

require 'test_helper'

class UserTest < ActiveSupport::TestCase
  should validate_presence_of :email
  should validate_format_of(:email).with("user+throwaway@subdom.example.com").with_message(/valid email address/)
  should validate_presence_of(:encrypted_password)
  should validate_confirmation_of :password              
end
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时ruby -Itest test\unit\user_test.rb,我收到以下错误:

 test/unit/user_test.rb:4:in `<class:UserTest>': undefined method `validate_presence_of' for UserTest:Class (NoMethodError)
Run Code Online (Sandbox Code Playgroud)

我没有正确设置什么?

shoulda ruby-on-rails-3.1

4
推荐指数
1
解决办法
1717
查看次数

rails/rspec没有看到匹配器

Rails 4.2.4,Rspec 3.3.1,shoulda-matcher 3.0.0.

我正进入(状态

  #...
  358) Participant validations 
       Failure/Error: it { should ensure_length_of(:coresp_country).is_at_most(255) }
       NoMethodError:
         undefined method `ensure_length_of' for #<RSpec::ExampleGroups::Participant::Validations:0x0000000f40aec0>
       # ./spec/models/participant_spec.rb:100:in `block (3 levels) in <top (required)>'

  359) Participant validations company 
       Failure/Error: it { should ensure_length_of(:company).is_at_most(255) }
       NoMethodError:
         undefined method `ensure_length_of' for #<RSpec::ExampleGroups::Participant::Validations::Company:0x0000000f414ab0>
       # ./spec/models/participant_spec.rb:149:in `block (4 levels) in <top (required)>'

  360) Participant validations company declared_type = COMPANY 
       Failure/Error: it { should validate_presence_of(:company) }
       NoMethodError:
         undefined method `validate_presence_of' for #<RSpec::ExampleGroups::Participant::Validations::Company::DeclaredTypeCOMPANY:0x0000000f429c58>
   #...
Run Code Online (Sandbox Code Playgroud)

还有更多此类故障(看起来像是不合适的).

rails_helper.rb:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails shoulda rspec-rails ruby-on-rails-4.2

4
推荐指数
1
解决办法
1867
查看次数

NoMethodError:未定义的方法`validate_presence_of'(Rspec和Shoulda-Matchers)

我正在尝试设置Rspec和Shoulda-Matchers,但出于某种原因我得到了这个错误:

NoMethodError:#RSpec的未定义方法`validate_presence_of':: ExampleGroups :: AdCampaign :: Validations:0x000000062a2b90>

失败/错误:{should have_many:advertisements}期望#响应 has_many?

NoMethodError:未定义的方法`belongs_to'用于#RSpec :: ExampleGroups :: AdCampaign :: Associations:0x0000000686d8e0>

似乎我尝试了stackoverflow和github问题的每个答案,没有任何帮助.

也许你可以帮我找出我做错了什么?

这是我的rails_helper.rb:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'shoulda/matchers'
require 'spec_helper'

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!
  config.filter_rails_from_backtrace!
end

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end
Run Code Online (Sandbox Code Playgroud)

这是我的spec_helper.rb:

ENV['RAILS_ENV'] ||= 'test'

require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails' …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails shoulda guard

4
推荐指数
1
解决办法
1783
查看次数

如何在Rails中测试模型是否具有给定方法?

我想实现这个方法User.calculate_hashed_password.我正在尝试使用与Rails的内置测试工具配合使用的Shoulda测试库,因此与Test :: Unit相关的答案与与Shoulda(我认为)相关的答案一样好.

我想弄清楚我需要测试什么以及我应该如何测试它.我最初的想法是做一些像......

class UserTest < ActiveSupport::TestCase
  should 'Return a hashed password'
    assert_not_nil User.calculate_hashed_password
  end
end
Run Code Online (Sandbox Code Playgroud)

这是正确的方法吗?

ruby testing unit-testing ruby-on-rails shoulda

3
推荐指数
2
解决办法
4458
查看次数

为什么这个Rails控制器测试失败?

我试图理解为什么这个测试失败了.(我对测试很陌生.)我正在使用内置的Rails测试框架并添加了Shoulda gem.

考试:

require 'shoulda'

context "on GET to :new" do

  setup do
    get(:new)
  end

  should_render_template :new
  should_not_set_the_flash

end
Run Code Online (Sandbox Code Playgroud)

失败:

1) Failure:
test: on GET to :new should render template :new. (SessionsControllerTest)
[/usr/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.0.6/lib/shoulda/controller   /macros.rb:220:in `__bind_1233882600_699194'
/usr/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.0.6/lib/shoulda/context.rb:254:in `call'
/usr/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.0.6/lib/shoulda/context.rb:254:in `test: on GET to :new should render template :new. '
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:94:in `__send__'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:94:in `run']:
expecting <"new"> but rendering with <"">

2 tests, 2 assertions, 1 failures, 0 errors
Run Code Online (Sandbox Code Playgroud)

但是,如果我在控制台上运行它,app.get '/sessions/new'它可以正常工作,没有错误.

并且" new"模板在浏览器中按预期呈现.

我正在使用Haml.也许这会导致问题.我的模板名为" new.html.haml".

testing tdd bdd ruby-on-rails shoulda

3
推荐指数
1
解决办法
2726
查看次数

如何在没有Shoulda的情况下在Rspec中进行一次线性测试?

我有一堆非常重复的rspec测试,它们都具有相同的格式:

it "inserts the correct ATTRIBUTE_NAME" do
     @o.ATTRIBUTE_NAME.should eql(VALUE)
end
Run Code Online (Sandbox Code Playgroud)

如果我能做一个像以下那样的线测试会很好:

compare_value(ATTRIBUTE_NAME, VALUE)
Run Code Online (Sandbox Code Playgroud)

但是,似乎并不适合这些类型的测试.还有其他选择吗?

ruby rspec ruby-on-rails shoulda

3
推荐指数
1
解决办法
1912
查看次数

如何使用 Shoulda 正确检查唯一性和范围

我有一个User具有items. 该:name项目的应该是用户唯一的,但它应该允许不同的用户拥有的项目具有相同名称。

Item 模型当前设置为:

class Item < ApplicationRecord
  belongs_to :user
  validates :name, case_sensitive: false, uniqueness: { scope: :user }
end
Run Code Online (Sandbox Code Playgroud)

这适用于验证内部用户,但仍允许其他用户使用相同的名称保存项目。

我如何使用 RSpec/Shoulda 进行测试?

我目前的测试是这样写的:

describe 'validations' do
    it { should validate_uniqueness_of(:name).case_insensitive.scoped_to(:user) }
  end
Run Code Online (Sandbox Code Playgroud)

但是这个测试失败了,因为:

Failure/Error: it { should validate_uniqueness_of(:name).scoped_to(:user).case_insensitive }

       Item did not properly validate that :name is case-insensitively
       unique within the scope of :user.
         After taking the given Item, setting its :name to ‹"an
         arbitrary value"›, and saving it as the existing …
Run Code Online (Sandbox Code Playgroud)

rspec shoulda

3
推荐指数
1
解决办法
2333
查看次数

Rspec/Rails并使用范围测试validates_uniquess_of

这是我的测试代码:

require 'spec_helper'

describe Classroom, focus: true do

  let(:user) { build_stubbed(:user) }

  describe "associations" do
    it { should belong_to(:user) }
  end

  describe "validations" do
    it { should validate_presence_of(:user) }
    it { should validate_uniqueness_of(:name).scoped_to(:user_id) }

    context "should create a unique classroom per user" do

      before(:each) do
        @class = build_stubbed(:classroom, name: "Hello World", user: user)
        @class2 = build_stubbed(:classroom, name: "Hello World", user: user)
      end

      it "should not create the second classroom" do
        @class2.should have(1).errors_on(:name)
      end
    end
  end

  describe "instance methods" do

    before(:each) …
Run Code Online (Sandbox Code Playgroud)

tdd rspec shoulda ruby-on-rails-3

2
推荐指数
1
解决办法
4275
查看次数

Shoulda_matches错误:#RSpec的未定义方法`validate_presence_of':: ExampleGroups :: Idea_3 :: Validations :: Title:0x007f3f88fa7548>

我正在学习TDD并尝试使用shoulda_matchers来帮助我进行测试,但是我得到了一个非常奇怪的错误.这是我的测试:

规格/型号/ idea_spec.rb

require 'rails_helper'

describe Idea do

  context 'Validations' do
    describe 'title' do
       it { should validate_presence_of(:title) }
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

测试错误说:

Idea Validations title 
 Failure/Error: it { should validate_presence_of(:title) }
 NoMethodError:
   undefined method `validate_presence_of' for #<RSpec::ExampleGroups::Idea_3::Validations::Title:0x007f056f9fcdf8>
 # ./spec/models/idea_spec.rb:7:in `block (4 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)

根据gem指令,require' Shoulda/matchers'位于我的rails_helper文件的顶部.

宝石我正在使用:

group :development, :test do
  gem 'spring'
  gem 'dotenv-rails'
  gem 'rspec-rails', '~> 3.0'
  gem 'factory_girl_rails'
end

group :test do
  gem 'webmock', '~> 1.20.4'
  gem 'shoulda-matchers', require: false
end
Run Code Online (Sandbox Code Playgroud)
  1. 红宝石'2.1.2'
  2. rails','4.1.9'

ruby unit-testing rspec ruby-on-rails shoulda

2
推荐指数
2
解决办法
2459
查看次数