Mongomapper - 在 Rails 2.3.5 上使用 Shoulda 进行单元测试
我正在尝试使用 mongomapper 在 Rails 2.3.5 应用程序上实现 shoulda 单元测试。
到目前为止,我已经:
- 配置了一个使用 mongomapper 的 Rails 应用程序(该应用程序可以工作)
- 将 Shoulda 添加到我的 gems 中,并使用
rake gems:install
安装它 - 添加了
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:
- Configured a rails app that uses mongomapper (the app works)
- Added shoulda to my gems, and installed it with
rake gems:install
- Added
config.frameworks -= [ :active_record, :active_resource
] toconfig/environment.rb
soActiveRecord
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过更深入的研究shoulda,我意识到了问题所在。
Shoulda 的宏(
should_belong_to
、should_have_many
、should_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.