Testing modules in RSpec
The rad way =>
let(:dummy_class) { Class.new { include ModuleToBeTested } }
Alternatively you can extend the test class with your module:
let(:dummy_class) { Class.new { extend ModuleToBeTested } }
Using 'let' is better than using an instance variable to define the dummy class in the before(:each)
What mike said. Here's a trivial example:
module code...
module Say def hello "hello" endend
spec fragment...
class DummyClassendbefore(:each) do @dummy_class = DummyClass.new @dummy_class.extend(Say)endit "get hello string" do expect(@dummy_class.hello).to eq "hello"end
For modules that can be tested in isolation or by mocking the class, I like something along the lines of:
module:
module MyModule def hallo "hallo" endend
spec:
describe MyModule do include MyModule it { hallo.should == "hallo" }end
It might seem wrong to hijack nested example groups, but I like the terseness. Any thoughts?