nil:NilClass

问题描述

我正在尝试使用基于Rack的RODA Ruby Web框架。但是,在框架上使用Minitest时遇到问题。 RSpec也很不喜欢Rails。我试图用撬动重现该错误,但我无法理解。我该如何解决?我正在

nil:NilClass的未定义方法“ entry_mapping”。

下面是相关代码

宝石文件

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 | 树路由

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

尝试调试

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?'

解决方法

我在 contentful_model-1.3.0 / lib / contentful_model / base.rb:124 中看到的代码是:

searchEditions

如果错误消息为editionResults,则

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

要解决此问题,您需要配置ContentfulModel。像这样的东西:

undefined method `entry_mapping' for nil:NilClass

使用此配置,此调用不再为空

ContentfulModel.configuration

请参见https://github.com/contentful/contentful_model