Skip to content

Commit

Permalink
Add more doc & ensure wcc/contentful is required
Browse files Browse the repository at this point in the history
  • Loading branch information
gburgett committed Oct 17, 2018
1 parent d121168 commit 85e15e5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
57 changes: 57 additions & 0 deletions wcc-contentful/README.md
Expand Up @@ -211,6 +211,63 @@ class MyJob < ApplicationJob
end
```

## Test Helpers

To use the test helpers, include the following in your rails_helper.rb:

```ruby
require 'wcc/contentful/rspec'
```

This adds the following helpers to all your specs:

```ruby
##
# Builds a in-memory instance of the Contentful model for the given content_type.
# All attributes that are known to be required fields on the content type
# will return a default value based on the field type.
instance = contentful_create('my-content-type', my_field: 'some-value')
# => #<WCC::Contentful::Model::MyContentType:0x0000000005c71a78 @created_at=2018-04-16 18:41:17 UTC...>

instance.my_field
# => "some-value"

instance.other_required_field
# => "default-value"

instance.other_optional_field
# => nil

instance.not_a_field
# NoMethodError: undefined method `not_a_field' for #<Menu:0x00007fbac81ee490>

##
# Builds a rspec double of the Contentful model for the given content_type.
# All attributes that are known to be required fields on the content type
# will return a default value based on the field type.
dbl = contentful_double('my-content-type', my_field: 'other-value')
# => #<Double (anonymous)>

dbl.my_field
# => "other-value"

##
# Builds out a fake Contentful entry for the given content type, and then
# stubs the Model API to return that content type for `.find` and `.find_by`
# query methods.
stubbed = contentful_stub('my-content-type', id: '1234', my_field: 'test')

WCC::Contentful::Model.find('1234') == stubbed
# => true

MyContentType.find('1234') == stubbed
# => true

MyContentType.find_by(my_field: 'test') == stubbed
# => true
```


## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
12 changes: 10 additions & 2 deletions wcc-contentful/lib/wcc/contentful/rspec.rb
@@ -1,11 +1,17 @@
# frozen_string_literal: true

require 'wcc/contentful'

require_relative './test'

module WCC::Contentful::RSpec
include WCC::Contentful::Test::Double
include WCC::Contentful::Test::Factory

##
# Builds out a fake Contentful entry for the given content type, and then
# stubs the Model API to return that content type for `.find` and `.find_by`
# query methods.
def contentful_stub(content_type, **attrs)
const = WCC::Contentful::Model.resolve_constant(content_type.to_s)
instance = contentful_create(content_type, **attrs)
Expand All @@ -28,6 +34,8 @@ def contentful_stub(content_type, **attrs)
end
end

RSpec.configure do |config|
config.include WCC::Contentful::RSpec
if defined?(RSpec)
RSpec.configure do |config|
config.include WCC::Contentful::RSpec
end
end

0 comments on commit 85e15e5

Please sign in to comment.