1

I'm using Benedikt Diecke's post on custom rspec example groups to test active model serializers but having trouble converting this to use mocha instead of rspec mocks.

http://benediktdeicke.com/2013/01/custom-rspec-example-groups/

The example includes a generic method to mock the model class that will be serialized, it uses rspec mocks - how can I convert this to use mocha?

let(:resource) do
    double(resource_name, attributes).tap do |double|
        double.stub(:read_attribute_for_serialization) { |name| attributes[name] }
    end
end

1 Answer 1

3

I can't see that there's much to be gained using a mock there. I'd return a real instance from the resource factory method, I.e. in the custom example group don't define let(:attributes) at all and just define

let(:resource) do
  {}
end

and then your serializer spec would look like

require 'spec_helper'

describe UserSerializer do
  let(:resource){ FactoryGirl.build(:resource_name) }

  it { should have_key(:name) }
  it { should have_key(:email) }
  it { should have_key(:created_at) }
  it { should have_key(:updated_at) }
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.