Ruby on rails 模块方法未定义方法的rspec测试委托
此模块包含在rails中的表单对象中 使用rspec测试它的正确方法是什么 1) 我是否直接在包含它的每个模型上测试它 或 2) 我是否直接测试委托方法?(如果可能的话,我更喜欢直达) 如果我直接测试,如何测试?我试图得到下面的错误 窗体对象模块 窗体对象 RSPEC测试 错误 失败/错误: 注册::基本::ActAsDelegation.delegate_字段_to(:用户,:地址)Ruby on rails 模块方法未定义方法的rspec测试委托,ruby-on-rails,ruby,rspec,delegation,ruby-on-rails-5.2,Ruby On Rails,Ruby,Rspec,Delegation,Ruby On Rails 5.2,此模块包含在rails中的表单对象中 使用rspec测试它的正确方法是什么 1) 我是否直接在包含它的每个模型上测试它 或 2) 我是否直接测试委托方法?(如果可能的话,我更喜欢直达) 如果我直接测试,如何测试?我试图得到下面的错误 窗体对象模块 窗体对象 RSPEC测试 错误 失败/错误: 注册::基本::ActAsDelegation.delegate_字段_to(:用户,:地址) (在本例中,我还有其他代码问题,但下面解决了主要问题)由于您的模块被设计为包含在测试中,只需将其包含在一个空类
(在本例中,我还有其他代码问题,但下面解决了主要问题)由于您的模块被设计为包含在测试中,只需将其包含在一个空类中即可。我准备了一个简化的示例,我验证了该示例的有效性:
module ToBeIncluded
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def a_class_method
:class
end
end
end
class TestSubject
include ToBeIncluded
end
require 'rspec/core'
RSpec.describe ToBeIncluded do
subject { TestSubject }
it 'returns correct symbol' do
expect(subject.a_class_method).to eq(:class)
end
end
在您的情况下,可能沿着这些思路的一些东西应该是好的:
require "rails_helper"
RSpec.describe Registration::Base::ActAsDelegation, type: :model do
class TestClass
include Registration::Base::ActAsDelegation
end
describe "Class Methods" do
context "#delegate_fields_to" do
let(:user) {spy('user')}
let(:address) {spy('address')}
let(:delegation_fields) { [
{name: :first, model: :user},
{name: :address, model: :address}
]}
it "should delegate" do
allow(TestClass).to receive(:form_fields_mapping) { delegation_fields }
TestClass.delegate_fields_to(:user,:address)
expect(user).to have_received(:first)
expect(address).to have_received(:address)
end
end
end
end
此外,如果您担心名称冲突,您可以创建匿名类。失败/错误:expect(user)。接收(:first)(双“user”)。first(*(任意参数))预期:收到任何参数1次:收到任何参数0次那么什么是匿名类?您不应该调用类似于
TestClass.new的东西吗。首先
,然后检查它是否委托给用户
?是的,在实际代码中已更正,现在一切正常。非常感谢。
require "rails_helper"
RSpec.describe Registration::Base::ActAsDelegation, type: :model do
describe "Class Methods" do
context "#delegate_fields_to" do
let(:user) {spy('user')}
let(:address) {spy('address')}
let(:delegation_fields) { [
{name: :first, model: :user},
{name: :address, model: :address}
]}
it "should delegate" do
allow(subject).to receive(:form_fields_mapping) { delegation_fields }
Registration::Base::ActAsDelegation.delegate_fields_to(:user,:address)
expect(user).to have_received(:first)
expect(address).to have_received(:address)
end
end
end
end
NoMethodError:
undefined method `delegate_fields_to' for Registration::Base::ActAsDelegation:Module
Did you mean? delegate_missing_to
module ToBeIncluded
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def a_class_method
:class
end
end
end
class TestSubject
include ToBeIncluded
end
require 'rspec/core'
RSpec.describe ToBeIncluded do
subject { TestSubject }
it 'returns correct symbol' do
expect(subject.a_class_method).to eq(:class)
end
end
require "rails_helper"
RSpec.describe Registration::Base::ActAsDelegation, type: :model do
class TestClass
include Registration::Base::ActAsDelegation
end
describe "Class Methods" do
context "#delegate_fields_to" do
let(:user) {spy('user')}
let(:address) {spy('address')}
let(:delegation_fields) { [
{name: :first, model: :user},
{name: :address, model: :address}
]}
it "should delegate" do
allow(TestClass).to receive(:form_fields_mapping) { delegation_fields }
TestClass.delegate_fields_to(:user,:address)
expect(user).to have_received(:first)
expect(address).to have_received(:address)
end
end
end
end