gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:39:07 26 4
gpt4 key购买 nike

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

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

请求.rb:

class Request < ApplicationRecord

validates_presence_of :userid, :supervisor, :status

has_many :servicerequests, dependent: :destroy
accepts_nested_attributes_for :servicerequests

end

服务.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)>'

我意识到 shoulda 还没有内置 optional ,但我想找出一种方法来测试它,但将它保留在那里。

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

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

最佳答案

感谢@Aguardientico 指出,我应该把

describe ServiceRequest 而不是 describe "ServiceRequests"

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com