Ruby – Testing modules in RSpec

rspecrubyunit testing

What are the best practices on testing modules in RSpec? I have some modules that get included in few models and for now I simply have duplicate tests for each model (with few differences). Is there a way to DRY it up?

Best Answer

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)

When to use RSpec let()?

Related Topic