Mongomapper - 在 Rails 2.3.5 上使用 Shoulda 进行单元测试

发布于 2024-09-03 20:38:27 字数 1089 浏览 3 评论 0原文

我正在尝试使用 mongomapper 在 Rails 2.3.5 应用程序上实现 shoulda 单元测试。

到目前为止,我已经:

  1. 配置了一个使用 mongomapper 的 Rails 应用程序(该应用程序可以工作)
  2. 将 Shoulda 添加到我的 gems 中,并使用 rake gems:install 安装它
  3. 添加了 config.frameworks -= [ : active_record, :active_resource ] 到 config/environment.rb,因此不使用 ActiveRecord

我的模型如下所示:

class Account
  include MongoMapper::Document

  key :name, String, :required => true
  key :description, String
  key :company_id, ObjectId
  key :_type, String

  belongs_to :company
  many :operations

end

我对该模型的测试是这样的:

class AccountTest < Test::Unit::TestCase

  should_belong_to :company
  should_have_many :operations

  should_validate_presence_of :name

end

它在第一个 should_belong_to 上失败:

./test/unit/account_test.rb:3: undefined method `should_belong_to' for AccountTest:Class (NoMethodError)

有什么想法为什么这不起作用?我应该尝试一些与应该不同的东西吗?

我必须指出,这是我第一次尝试使用 Shoulda,而且我对测试本身还很陌生。

I'm trying to implement shoulda unit tests on a rails 2.3.5 app using mongomapper.

So far I've:

  1. Configured a rails app that uses mongomapper (the app works)
  2. Added shoulda to my gems, and installed it with rake gems:install
  3. Added config.frameworks -= [ :active_record, :active_resource ] to config/environment.rb so ActiveRecord isn't used.

My models look like this:

class Account
  include MongoMapper::Document

  key :name, String, :required => true
  key :description, String
  key :company_id, ObjectId
  key :_type, String

  belongs_to :company
  many :operations

end

My test for that model is this one:

class AccountTest < Test::Unit::TestCase

  should_belong_to :company
  should_have_many :operations

  should_validate_presence_of :name

end

It fails on the first should_belong_to:

./test/unit/account_test.rb:3: undefined method `should_belong_to' for AccountTest:Class (NoMethodError)

Any ideas why this doesn't work? Should I try something different from shoulda?

I must point out that this is the first time I try to use shoulda, and I'm pretty new to testing itself.

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

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

发布评论

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

评论(1

经过更深入的研究shoulda,我意识到了问题所在。

Shoulda 的宏(should_belong_toshould_have_manyshould_validate_presence_of)仅适用于 ActiveRecord - 毕竟它们是在 Shoulda::ActiveRecord::Macros

如果我要使用它们,我将必须为 Shoulda::MongoMapper::Macros 实现宏。我不确定这是否值得。

我希望这对找到这篇文章的人有所帮助。

After studying shoulda more profoundly, I realized what was wrong.

Shoulda's macros (should_belong_to, should_have_many, should_validate_presence_of) are only available for ActiveRecord - after all they are defined on Shoulda::ActiveRecord::Macros.

If I were to use those, I would have to implement macros for Shoulda::MongoMapper::Macros. I'm not sure it is worth it.

I hope this helps anyone finding this post.

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