0

我正在尝试使用基于 Rack 的 RODA Ruby Web 框架。但是,我在使用Minitest框架时遇到问题。与 Rails 不同,RSpec 也很忙。我试图用 pry 重现错误,但我无法理解它。我应该如何解决这个问题?我正进入(状态

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

模型/recipe.rb

require 'contentful_model'

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

  def self.all_recipes
    all.load!
  end
end

应用程序.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_helper.rb

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

require_relative '../minitest_helper'

规格/模型/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?'
4

1 回答 1

1

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

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

如果错误消息是undefined method `entry_mapping' for nil:NilClass那么

ContentfulModel.configuration

是零

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

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

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

ContentfulModel.configuration

https://github.com/contentful/contentful_model

于 2020-09-24T16:13:14.110 回答