【发布时间】: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