ruby-on-rails - 应该使用 Rspec Gem 返回字符串 :Class"in belong_to test 的 "undefined method ` Reflect_on_association'

标签 ruby-on-rails testing rspec shoulda

在我的 Rails 应用程序中,我有模型 RequestServiceServiceRequest

在我的 models.rb 文件中,我有:

request.rb:

class Request < ApplicationRecord

  validates_presence_of :userid, :supervisor, :status 

  has_many :servicerequests, dependent: :destroy
  accepts_nested_attributes_for :servicerequests

end

service.rb:

class Service < ApplicationRecord
  validates_presence_of :title, :responsible

  has_many :servicerequests, dependent: :destroy
end

servicerequest.rb:

class Servicerequest < ApplicationRecord
  belongs_to :request, optional: true
  belongs_to :service, optional: true
end

以及导致问题的规范servicerequest_spec.rb:

require "rails_helper"

describe "ServiceRequests", :type => :model do 
  it "is valid with valid attributes"
  it "is not valid without a userid"
  it "is not valid without a request_id"
  it "is not valid without a service_id"

  it { should belong_to(:request)}
  it { should belong_to(:service)}
end

这两行具体是:

it { should belong_to(:request)}
it { should belong_to(:service)}

我收到错误:

     NoMethodError:
   undefined method `reflect_on_association' for String:Class
 # /Users/criva/.rvm/gems/ruby-2.3.1@onboard/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb:21:in `reflect_on_association'
 # /Users/criva/.rvm/gems/ruby-2.3.1@onboard/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb:17:in `reflection'
 # /Users/criva/.rvm/gems/ruby-2.3.1@onboard/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_record/association_matcher.rb:825:in `reflection'
 # /Users/criva/.rvm/gems/ruby-2.3.1@onboard/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_record/association_matcher.rb:993:in `association_exists?'
 # /Users/criva/.rvm/gems/ruby-2.3.1@onboard/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_record/association_matcher.rb:926:in `matches?'
 # ./spec/models/servicerequest_spec.rb:10:in `block (2 levels) in <top (required)>'

我意识到应该还没有内置可选,但我希望找到一种方法来测试它,但仍将其保留在那里。

任何帮助都会有助于解决这个谜团。

我尝试过 it { should follow_to(:request).optional(true)}it { should follow_to(:request).conditions(Optional: true)} 无济于事。

最佳答案

感谢@Aguardientico 指出,我应该把

描述ServiceRequest而不是描述“ServiceRequests”

关于ruby-on-rails - 应该使用 Rspec Gem 返回字符串 :Class"in belong_to test 的 "undefined method ` Reflect_on_association',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40877751/

相关文章:

ruby-on-rails - Rails,从 NEW 重定向到 CREATE

php - 如何在 php 测试中使用模拟对象

java - Spring JUnit 测试在我的服务类上保持返回 nullpointerexception

unit-testing - 如果一个方法调用另一个方法,是单元测试还是集成测试?

ruby-on-rails - 无法让 RSpec View 规范通过

javascript - 复杂形式 - 如何为每个新元素生成新的 object_ids?

ruby-on-rails - 将文件从 Amazon S3 复制到另一个 FTP 的最佳方法是什么?

ruby-on-rails - 如何测试一个 before_save 方法,包括与 rspec 的关联

ruby-on-rails - ruby on Rails 比较两个日期

ruby-on-rails - 在 Ruby on Rails 应用程序中使用 RSpec 时,对 Mock 对象的 #class 方法进行 stub 是否合法?