0
在測試模塊的rspec測試套件中,測試用於評估包含該模塊的類的行爲。下面是一個粗略的例子來演示。在rspec中,將類名傳遞給shared_example的正確方法是什麼?
describe "MyMod" do
module MyMod; end
shared_examples_for "a class" do |klass|
it "includes the module" do
klass.included_modules.include? MyMod.should be_true
end
end
describe "when included in a class" do
before(:each) { class C; include MyMod; end }
it_behaves_like "a class", C
end
describe "when extended in a class" do
before(:each) { class C; extend MyMod; end }
it_behaves_like "a class", C
end
end
我已經是當執行it_behaves_like
類C
沒有定義,產生一個錯誤的問題:
`block (2 levels) in <top (required)>': uninitialized constant C (NameError)
什麼是正確的方式來定義一個類,並把它傳遞給一個shared_example ?
是的,謝謝,我從閱讀本書。我嘗試過爲'extend'工作的'let(:klass){Class.new.extend(MyMod)}',但給include包括了一個錯誤:「private method'include'called'。我想我只會用一種不同的方法! – starfry