【问题标题】:How to test spree decorator in original app?如何在原始应用程序中测试狂欢装饰器?
【发布时间】:2019-02-20 13:51:03
【问题描述】:

我正在使用 Spree 制作名为“MyApp”的 Rails 应用程序,并尝试使用 RSpec 进行测试。

我阅读了 Spree Test App Document,并尝试使用 RSpec 进行测试。

但我有错误

~/r/d/MyApp ❯❯❯ bundle exec rspec                                                             

An error occurred while loading ./spec/controllers/spree/home_controller_spec.rb.
Failure/Error:
  describe Spree::HomeController, type: :controller do
    it "render index template" do
      get :index
      response.should render_template(:index)
    end
  end

NameError:
  uninitialized constant Spree
# ./spec/controllers/spree/home_controller_spec.rb:3:in `<top (required)>'
No examples found.

Finished in 0.0004 seconds (files took 0.146 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

NameError: 未初始化的常量 Spree 我认为发生这个错误是因为它只定义了装饰器。 原始控制器在 Spree gem(core, backend, frontend ...etc)中定义。

详细代码如下:

MyApp/app/controllers/home_controller_decorator.rb

Spree::HomeController.class_eval do
  def index
    do_something
  end
end

MyApp/specs/controllers/home_controller_spec.rb

require 'spec_helper'

describe Spree::HomeController do
  it "render index template" do
    get :index
    response.should render_template(:index)
  end
end

MyApp/spec/spec_helper.rb

RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.shared_context_metadata_behavior = :apply_to_host_groups
  config.disable_monkey_patching!
  config.order = :random
  Kernel.srand config.seed
end

如何测试我的装饰器原始逻辑? 我应该在哪里写代码? 无法测试 MyApp/spec 目录?

也许我对 Spree Test 有误解。

请给我提示。 谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby spree


    【解决方案1】:

    问题

    它没有加载 Rails,所以它看不到 Spree。

    解决方案

    MyApp/specs/controllers/home_controller_spec.rb

    代替

    require 'spec_helper'
    
    describe Spree::HomeController do
      # ...
    

    你需要

    require 'rails_helper'
    
    describe Spree::HomeController do
      # ...
    

    更多信息

    • spec_helper 旨在设置 RSpec 以准备测试普通 Ruby 对象
    • 如果您的测试代码位于 lib 文件夹中并且不依赖于 Rails,则可以这样做
    • 由于Spree完全依赖Rails,需要通过require 'rails_helper'加载Rails
    • rails_helper 也需要 spec_helper 但反过来不正确
    • rails_helper 的加载速度比 spec_helper 慢得多,正是因为它启动了 Rails

    rails_helper的剖析

    • Rails 5 版本
    • 您的 rails_helper 可能看起来不一样
    # Load spec_helper
    require 'spec_helper'
    
    # Setup Rails to run in test mode
    ENV['RAILS_ENV'] ||= 'test'
    
    # This is the magic line that runs Rails. Check out config/environment.rb.
    require File.expand_path('../../config/environment', __FILE__)
    
    # Guard against running any tests in production mode
    abort("The Rails environment is running in production mode!") if Rails.env.production?
    
    # Load all the rspec rails related goodness
    require 'rspec/rails'
    
    # Make sure the migrations are up to date
    begin
      ActiveRecord::Migration.maintain_test_schema!
    rescue ActiveRecord::PendingMigrationError => e
      puts e.to_s.strip
      exit 1
    end
    

    【讨论】:

    • 哦..我有一个非常简单的错误...它有效。我很抱歉犯了一个基本错误。谢谢大家的好意!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 2016-06-04
    • 2017-08-20
    • 2017-07-17
    • 1970-01-01
    相关资源
    最近更新 更多