如何重用这些示例,以便只覆盖嵌套上下文的详细信息?
像这样的东西(我使用thee
代替它,表明它是在嵌套的上下文中执行的。它不在RSpec中,正如我想要的那样):
describe "Abilities" do
subject { Abilities.new user }
context "allowed" do
let(:user) { Factory(:power_user) }
thee { should be_able_to :create, object }
thee { should be_able_to :read, object }
thee { should be_able_to :update, object }
context "comment" do
let(:object) { Factory(:comment) }
end
context "post" do
let(:object) { Factory(:post) }
end
context "blog" do
let(:object) { Factory(:blog) }
end
end
end
此示例最终将为3个上下文(评论,帖子,博客)提供3个示例(创建,阅读,更新),总共产生9个示例。
如何实现它(不编写共享示例)?
答案 0 :(得分:5)
没有办法继承这些示例,但您可以创建一个类方法:
describe "Abilities" do
subject { Abilities.new user }
def self.should_allow_stuff
it { should be_able_to :create, object }
it { should be_able_to :read, object }
it { should be_able_to :update, object }
end
context "allowed" do
let(:user) { Factory(:power_user) }
context "comment" do
let(:object) { Factory(:comment) }
should_allow_stuff
end
context "post" do
let(:object) { Factory(:post) }
should_allow_stuff
end
context "blog" do
let(:object) { Factory(:blog) }
should_allow_stuff
end
end
end
如果您愿意,可以根据需要进行重构。
答案 1 :(得分:1)
为什么不想编写共享示例?这正是他们的目的。