【发布时间】:2020-02-11 15:19:58
【问题描述】:
我正在编写 Rails 扩展。为了测试它,我使用 RSpec。为了制作模型来测试插件,我使用了一个预生成的虚拟应用程序:
rails plugin new yaffle --dummy-path=spec/dummy --skip-test --full
我有一些测试。当我调用它们时,它们会运行两次。
> # I have 4 tests in app total
> rspec
> 8 examples, 0 failures
> rspec spec/yaffle/active_record/acts_as_yaffle_spec.rb
> 4 examples, 0 failures
> rspec spec/yaffle/active_record/acts_as_yaffle_spec.rb:4
> 2 examples, 0 failures
这就是我的文件的样子:
# lib/yaffle.rb
Gem.find_files('yaffle/**/*.rb').each { |path| require path }
module Yaffle
# Your code goes here...
end
# spec/spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../dummy/config/environment", __FILE__)
RSpec.configure do |config|
config.example_status_persistence_file_path = '.rspec_status'
config.disable_monkey_patching!
end
# spec/dummy/config/environment.rb
# Load the Rails application.
require_relative 'application'
# Initialize the Rails application.
Rails.application.initialize!
# spec/yaffle/active_record/acts_as_yaffle_spec.rb
require "spec_helper"
RSpec.describe "Acts as yaffle" do
def test_a_hickwalls_yaffle_text_field_should_be_last_squawk
assert_equal "last_squawk", Hickwall.yaffle_text_field
end
def test_a_wickwalls_yaffle_text_field_should_be_last_tweet
assert_equal "last_tweet", Wickwall.yaffle_text_field
end
end
# spec/dummy/config/application.rb
require_relative 'boot'
require "rails/all"
Bundler.require(*Rails.groups)
require "yaffle"
module Dummy
class Application < Rails::Application
config.load_defaults 6.0
end
end
另外,我注意到只有带有require "spec_helper" 的文件是重复的
那么,我做错了什么?而且,如果它是一个错误,如何解决它?
【问题讨论】:
-
我会使用 Zeitwork 而不是使用
Gem.find_files急切地加载所有内容。
标签: ruby-on-rails ruby activerecord rspec