给定两个使用模板方法设计模式的类:
def Parent
def all_params
params.merge(extra_params)
end
def params
{blah: "cool"}
end
def extra_params
{}
end
end
def Child < Parent
def extra_params
{edmund: "coolest"}
end
end
在 Rspec 中测试这个的正确方法是什么?我应该shared_examples_for "a parent"
像这样创建然后it_should_behave_like 'a parent'
在两个类中使用测试吗?:
shared_examples_for "a parent" do
describe "#params" do
expect(subject.params).to eq (...)
end
describe "#all_params" do
expected_params = subject.all_params.merge(subject.extra_params)
expect(subject.all_params).to eq expected_params
end
end
describe Parent do
subject { Parent.new }
it_should_behave_like 'a parent'
describe "#extra_params" do
expect(subject.extra_params).to eq {}
end
end
describe Child do
subject { Child.new }
it_should_behave_like 'a parent'
describe "#extra_params" do
expect(subject.extra_params).to eq {edmund: 'coolest'}
end
end
或者我应该测试它Child
是父级,并且只测试它覆盖的钩子方法?
describe Parent do
subject { Parent.new }
describe "#params" do
expect(subject.params).to eq (...)
end
describe "#all_params" do
expected_params = subject.all_params.merge(subject.extra_params)
expect(subject.all_params).to eq expected_params
end
describe "#extra_params" do
expect(subject.extra_params).to eq {}
end
end
describe Child do
subject { Child.new }
it "is a Parent" do
expect(subject).to_be kind_of(Parent)
end
describe "#extra_params" do
expect(subject.extra_params).to eq {edmund: 'coolest'}
end
end