使用 shoulda 时出现 NoMethodError validate_presence_of

发布于 2024-12-04 18:11:36 字数 1300 浏览 1 评论 0 原文

好吧,我很困惑。我尝试在 Rails 3.1 下将 shoulda 与 Test::Unit 一起使用,之前在 Rails 2.3.11 中成功做到了这一点。

我的 Gemfile 中有以下内容:(

group :test do
  gem 'shoulda'
end

并且我已经运行 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

和以下 user_test.rb

require 'test_helper'

class UserTest < ActiveSupport::TestCase
  should validate_presence_of :email
  should validate_format_of(:email).with("[email protected]").with_message(/valid email address/)
  should validate_presence_of(:encrypted_password)
  should validate_confirmation_of :password              
end

但是当我执行 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)

我未能正确设置什么?

Ok, I'm baffled. I'm trying to use shoulda with Test::Unit under Rails 3.1, having previously done so successfully with Rails 2.3.11.

I have the following in my Gemfile:

group :test do
  gem 'shoulda'
end

(and I've run bundle install - bundle show shoulda shows c:/Ruby192/lib/ruby/gems/1.9.1/gems/shoulda-2.11.3)

I have the following test_helper.rb

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

class ActiveSupport::TestCase
end

and the following user_test.rb

require 'test_helper'

class UserTest < ActiveSupport::TestCase
  should validate_presence_of :email
  should validate_format_of(:email).with("[email protected]").with_message(/valid email address/)
  should validate_presence_of(:encrypted_password)
  should validate_confirmation_of :password              
end

But when I do ruby -Itest test\unit\user_test.rb, I get the following error:

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

What have I failed to set up properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天赋异禀 2024-12-11 18:11:36

解决了。您需要:

require 'shoulda/rails'

test_helper.rb 中(而不是 `require 'shoulda');并且测试用例必须是:(

class Usertest < ActiveRecord::TestCase

不是 )。

我单独尝试过这两个,但没有一起尝试......

Solved it. You need:

require 'shoulda/rails'

in test_helper.rb (not `require 'shoulda'); and the test case needs to be:

class Usertest < ActiveRecord::TestCase

(not < ActiveSupport::TestCase).

I'd tried both of those individually, but not together...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文