简体   繁体   中英

Are *all* specs for an engine expected to live in the dummy Rails app?

I'm using RSpec to test some engine models.

My preference would be to test the parts that are independent from the (dummy) app outside of the app. I'd prefer to have non-app tests live at the top level and not be hidden in spec/dummy/spec .

The problem is that by default (AFAICT) engine initializers aren't run if the engine isn't mounted.

Should I just go with what appears to be expected, ie, put all my specs in the dummy app and run RSpec from the dummy app's directory, even when the tests aren't related to the app as a whole?

Or should I run initializers from a top-level spec helper for the non-app specs? Or some other way?

If I do, are there dummy app repercussions?

If you have a dummy app, you do not have to have your specs in the dummy app dir structure.

The following is a simplified version of what permitters v0.0.1 uses.

In spec/spec_helper.rb :

ENV['RAILS_ENV'] = 'test'
app_path = File.expand_path("../dummy", __FILE__)
$LOAD_PATH.unshift(app_path) unless $LOAD_PATH.include?(app_path)

# if require rails, get uninitialized constant ActionView::Template::Handlers::ERB::ENCODING_FLAG (NameError)
require 'rails/all'
require 'config/environment'
require 'db/schema'
require 'rails/test_help'
require 'rspec/rails'

# rspec config, etc.

Beyond that, I want to say that all of the modifications I made in the dummy app in spec/dummy were either to allow it to be run in different versions of Rails (3.1.x, 3.2.x, and 4.0.x) or because I was configuring things for the gem in the dummy app.

I also currently like using the appraisal gem and TravisCI for continuous integration. The setup I'm using allows me to test in various versions of Rails with various versions of gems and not a lot of maintenance overhead. It needs a little cleanup, but it works well.

If you wanted to not load the Rails environment for a certain set of specs (ie not load Rails for some specs_, you could definitely do that. You could just set an env var in the task definition in the Rakefile or at command-line and then look for that in spec_helper.rb to determine whether to load things or not. Then you could have various Rake tasks that spawn new processes that set the env var or not depending on whether a set of tests needs Rails. I wouldn't necessarily worry about that though if everything is meant to be run in Rails, unless you really need to isolate it.

For more info on different ways to test with dummy apps, you might see this question: Strategies for gem tests to ensure the gem works with Rails 3.x and 4.0 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM