Search code examples
rubyminitestroda

undefined method `entry_mapping' for nil:NilClass


I am trying to play around with the RODA Ruby web framework based on Rack. However, I am having issues with using Minitest with the framework. RSpec was hectic with it too unlike Rails. I tried to reproduce the error with pry, but I couldn't make sense of it. How should I fix this? I am getting

undefined method `entry_mapping' for nil:NilClass.

Below is the associated code:

Gemfile

gem 'contentful_model', '~> 1.3'           # ActiveModel-like wrapper for the Contentful SDKs


group :test do
  gem 'capybara'
  gem 'minitest', '>= 5.7.0'
  gem 'minitest-hooks', '>= 1.1.0'
  gem "minitest-global_expectations"
  gem "warning"
  gem 'pry'
end

models/recipe.rb

require 'contentful_model'

class Recipe < ContentfulModel::Base
  self.content_type_id = 'recipe'

  def self.all_recipes
    all.load!
  end
end

app.rb | Tree Routing

require './.env' if File.exist?(".env.rb")
require './config/initializers/contentful_model'
require 'roda'
require './models/recipe'
require 'tilt/sass'

class App < Roda
  hash_routes do
    view '', 'index'
  end

  route do |r|
    @recipes = Recipe.all_recipes

    r.public
    r.assets
    check_csrf!
    r.hash_routes('')

    r.get String do |id|
      @recipe_details = Recipe.find(id)
      next unless @recipe_details
      view 'show'
    end
  end
end

spec/models/spec_helper.rb

ENV["RACK_ENV"] = "test"
require_relative '../../models/recipe'

require_relative '../minitest_helper'

spec/models/recipe_spec.rb

require_relative 'spec_helper'

describe Recipe do
  let(:recipes) { Recipe.all_recipes }
  describe '.all_recipes' do
    it 'return all records with content type recipe' do
      recipes = Recipe.all_recipes # undefined method `entry_mapping' for nil:NilClass
      expect(recipes).to all must_be Recipe
    end
  end
end

pry debug

From: /Users/tiwa/RubymineProjects/marley-spoon-roda/spec/models/recipe_spec.rb:27 .all_recipes#test_0001_return all records with content type recipe:

    24: it 'return all records with content type recipe' do
    25:   recipes = Recipe.all_recipes # undefined method `entry_mapping' for nil:NilClass
    26:   binding.pry
 => 27:   expect(recipes).to all must_be Recipe
    28: end

[1] pry(#<.all_recipes>)> Recipe.all_recipes
NoMethodError: undefined method `entry_mapping' for nil:NilClass
from /Users/tiwa/.gem/ruby/2.7.1/gems/contentful_model-1.3.0/lib/contentful_model/base.rb:124:in `mapping?'

Solution

  • I see the code in contentful_model-1.3.0/lib/contentful_model/base.rb:124 is:

    ContentfulModel.configuration.entry_mapping.key?(@content_type_id)
    

    If error msg is undefined method `entry_mapping' for nil:NilClass then

    ContentfulModel.configuration
    

    Is nil

    To fix this you need configure ContentfulModel. Something like that:

    ContentfulModel.configure do |config|
      config.access_token = "your access token in here" # Required
      config.preview_access_token = "your preview token in here" # Optional - required if you want to use the preview API
      config.management_token = "your management token in here" # Optional - required if you want to update or create content
      config.space = "your space id in here" # Required
      config.environment = "master" # Optional - defaults to 'master'
      config.default_locale = "en-US" # Optional - defaults to 'en-US'
      config.options = { # Optional
        # Extra options to send to the Contentful::Client and Contentful::Management::Client
        # See https://github.com/contentful/contentful.rb#configuration
    
        # Optional:
        # Use `delivery_api` and `management_api` keys to limit to what API the settings
        # will apply. Useful because Delivery API is usually visitor facing, while Management
        # is used in background tasks that can run much longer. For example:
        delivery_api: {
          timeout_read: 6
        },
        management_api: {
          timeout_read: 100
        }
      }
    end
    

    With this config this call is not more nil

    ContentfulModel.configuration
    

    See https://github.com/contentful/contentful_model