activerecord - 在非 Rails 应用程序中测试 ActiveRecord 与 Shoulda 的关联

标签 activerecord rspec sinatra shoulda

应用程序:Sinatra + ActiveRecord
尝试使用最佳实践来测试关联的存在。
我真的很喜欢 Shoulda 语法:

    describe Bar do
      it { should belong_to(:foo) }
    end

但是,RSpec 似乎找不到 belong_to 方法。

    1) ResOutcome 
       Failure/Error: it { should belong_to(:foo) }
       NoMethodError:
         undefined method `belong_to' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000103ac6760>
       # ./spec/models/bar_spec.rb:48:in `block (2 levels) in <top (required)>'

有什么提示吗?
关联测试是否应该只在 Rails 应用程序中工作(使用 rspec-rails)?
感谢大家抽出宝贵的时间。

最佳答案

您是否将 shoulda-matchers gem 添加到您的 Gemfile 中? https://github.com/thoughtbot/shoulda-matchers


更新:

require 'shoulda/matchers 添加到您的 spec_helper.rb 配置中。

关于activerecord - 在非 Rails 应用程序中测试 ActiveRecord 与 Shoulda 的关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11035311/

相关文章:

ruby-on-rails - Rails3 : How do I visit a subdomain in a steak(rspec) spec using Capybara

ruby - 使用 Ruby/Sinatra 和 JRuby 或 Scala 构建 REST API

ruby-on-rails - Rails 未定义的局部变量或方法 `line_item'

ruby-on-rails - 如何在连接表中按条件接收对象

ruby - 在 RSpec 测试中模拟 ActiveRecord 关系行为

ruby - RSpec - 如何测试对象是否在 #initialize 中向自身发送消息

ruby-on-rails - Rails + Sinatra 应用分享会

deployment - 权限拒绝了 Cap 的公钥,但我可以 ssh 进入 github

ruby-on-rails - 给定 Rails 5 has_and_belongs_to_many,如何创建连接记录?

mysql - 创建轮值表的用户