【问题标题】:Mongomapper - unit testing with shoulda on rails 2.3.5Mongoapper - 在 rails 2.3.5 上使用 shoulda 进行单元测试
【发布时间】:2010-06-02 12:55:00
【问题描述】:

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

到目前为止,我已经:

  1. 配置了一个使用 mongomapper 的 rails 应用程序(该应用程序有效)
  2. 将 shoulda 添加到我的 gem,并使用 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 不同的东西吗?

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

【问题讨论】:

    标签: ruby-on-rails tdd shoulda mongomapper


    【解决方案1】:

    在深入研究之后,我意识到了问题所在。

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

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

    我希望这可以帮助任何人找到这篇文章。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多