fbpx

26 Mar /Testing Modules in Rspec

Posted by Alex Miller

There comes a time when we might need to write some unit tests for a module. To do this, we can just extend some class and test the methods on that class. Here is an example using Rspec’s subject class; extending it with SomeProject::MyHelpers to unit test a method called :helpfulness.

require 'spec_helper'

describe SomeProject::MyHelpers do

  # add the module methods as instance methods to Rspec's subject
  subject do
    self.extend(described_class)
  end

  describe 'The helpfulness of my helper' do
    it 'helps a lot' do
      expect(subject.helpfulness).to eq "a lot"
    end
  end
end