Search code examples
ruby-on-railsstubbingcontentful

What's a good way to stub Contentful Model instances in feature specs?


(I think this question generalises to stubbing any extensively-pinged API, but I'm asking the question based on the code I'm actually working with)

We're using the Contentful Model extensively in our controllers and views including in our layouts. This means that in any feature test where we visit (say) the homepage, our controller action will include something like this:

class HomepageController < ApplicationController
  def homepage
    # ... other stuff
    @homepage_content = Homepage.find ('contentful_entry_id')
  end
end

... where Homepage is a subclass of ContentfulModel::Base, and @homepage_content will have various calls on it in the view (sometimes chained). In the footer there's a similar instance variable set and used repeatedly.

So for feature testing this is a pain. I've only come up with two options:

  1. Stub every single call (dozens) on all Contentful model instances, and either stub method chains or ensure they return a suitable mock

or

  1. Use a gem like VCR to store the Contentful responses for every feature spec

Both of these (at least the way I'm doing them) have pretty bad drawbacks:

1) leads to a bunch of test kruft that will have to be updated every time we add or remove a field from the relevant model;

2) means we generate a vcr yaml files for every feature test - and that we have to remember to clear the relevant yml file whenever we change an element of the test that would change the requests it sends

Am I missing a third option? Or is there some sensible way to do either of the above options without getting the main drawbacks?


Solution

  • I'm the maintainer of contentful_model.

    We use VCR to stub API Calls, so that you can test with real data and avoid complicated test code.

    Cheers