Ruby on rails Rspec Gem返回时是否应返回;未定义的方法`在关联上反映';对于字符串:Class";你应该去测试一下

Ruby on rails Rspec Gem返回时是否应返回;未定义的方法`在关联上反映';对于字符串:Class";你应该去测试一下,ruby-on-rails,testing,rspec,shoulda,Ruby On Rails,Testing,Rspec,Shoulda,在我的rails应用程序中,我有我的型号Request、Service、和ServiceRequest 在my models.rb文件中,我有: request.rb: class Request < ApplicationRecord validates_presence_of :userid, :supervisor, :status has_many :servicerequests, dependent: :destroy accepts_nested_attri

在我的rails应用程序中,我有我的型号
Request
Service
、和
ServiceRequest

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

request.rb

class Request < ApplicationRecord

  validates_presence_of :userid, :supervisor, :status 

  has_many :servicerequests, dependent: :destroy
  accepts_nested_attributes_for :servicerequests

end
class Service < ApplicationRecord
  validates_presence_of :title, :responsible

  has_many :servicerequests, dependent: :destroy
end
class Servicerequest < ApplicationRecord
  belongs_to :request, optional: true
  belongs_to :service, optional: true
end
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
servicerequest.rb

class Request < ApplicationRecord

  validates_presence_of :userid, :supervisor, :status 

  has_many :servicerequests, dependent: :destroy
  accepts_nested_attributes_for :servicerequests

end
class Service < ApplicationRecord
  validates_presence_of :title, :responsible

  has_many :servicerequests, dependent: :destroy
end
class Servicerequest < ApplicationRecord
  belongs_to :request, optional: true
  belongs_to :service, optional: true
end
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)>'
命名错误:
String:Class的未定义方法“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: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\u record/association\u 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:在“association_exists”中
#/Users/criva/.rvm/gems/ruby-2.3。1@onboard/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active\u record/association\u matcher.rb:926:in'matches'
#./spec/models/servicerequest_spec.rb:10:in'block(2层)in'
我意识到shoulda还没有内置
可选的
,但我希望找到一种方法来测试它,并将它保留在那里

任何帮助都将有助于解决这一迷雾


我尝试了
it{应该属于(:请求)。可选(true)}
it{应该属于(:请求)。条件(可选:true)}
无效。

感谢@Aguardientico指出,我应该把


描述服务请求
而不是
描述“服务请求”

如果您使用
描述服务请求
而不是
描述“服务请求”
该怎么办。。。。这非常有效。