I just created a mountable engine. The test directory looks somewhat like this:
engine_name
-test
- dummy
- test_helper.rb
- models
-engine_name
-- user_test.rb
etc
I attempted to call 'rspec' -> didn't work. Error message stated it couldn't find the folder 'spec'
I then called 'rspec test' -> it found the directory, but didn't find the test.
Found that rspec looks for *_spec.rb files, so I renamed user_test.rb to user_spec.rb
rspec then found the file, but it said it couldn't find test_helper.rb (since that was required by user_spec.rb).
I have a feeling that this engine's test directory is not designed for rspec. What's the best way to test an engine?
Thanks!
Since you are using rspec, you should use the directory spec instead of test.
The test directory is the default for test unit.
If you run rspec and you have a spec directory it will grab the tests from there.
You need a right spec_helper.rb (test_helper.rb is for test unit). You can see the example of spec_helper.rb in the template from rspec-rails repository.
Then, you should require the spec_helper in the user_spec.rb and just run rspec in the terminal.
Related
I have problem with rswag spec/integration/*_spec.rb files witch goes in the main thread when I run rspec. In my project I use rswag lib only for creating documentation but not testing.
Result: rspec - run all tests except rswag. And rake rswag run only spec/integration/* for documentation.
The quickest solution I could figure out was running tests under a specific folder. In my case I have integration tests under spec/ folder and rswag tests under spec/requests/, so I can do it like this: rspec --pattern spec/*_spec.rb
In your case should be rspec --pattern spec/integration/*_spec.rb.
See more about the --pattern option here.
I have a Rails 4.1 project that uses RSpec and Cucumber.
I've recently added fixture_builder.
fixture_builder.rb includes logic to rebuild fixtures any time the file changes.
This works fine for RSpec with require 'fixture_builder' in spec_helper.rb.
However, when running Cucumber tests fixture_builder.rb is not called, so fixtures are not updated if any changes have been made to fixture_builder.rb.
Is there an equivalent config file like spec_helper.rb for Cucumber?
By default, Cucumber loads all *.rb files in the same directory as the feature(s) it's running and all subdirectories of that directory, so you can put your require in any file you want in any of those directories.
The conventional thing to do is to put 'support' code like require in features/support/env.rb, or in another file in features/support if your env.rb gets too big.
The cucumber-rails gem provides a generator that sets up Cucumber to work with Rails. If you haven't already, install the gem and run
rails g cucumber:install
to create features/support/env.rb and the rest of the usual Rails + Cucumber setup.
No matter what I do, the only tests that run with the rake test command are those in spec/requests. Naturally, I would like to run everything in the spec directory.
I thought getting the gem and installing RSpec would do it, but it seems with these testing libraries that the whole "convention over configuration" thing is turned on its head. There's a hell of a lot of configuration.
I simply want to run all of my tests. How can I do that?
What does your Rakefile look like? You may need to add the following:
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new
Also, check that your spec's filenames all end in _spec.rb.
Just this will do:
bundle exec rspec spec
This must be fired from root directory of application. Here spec is the directory.
I have a Rails project, in which I have a library with specs, lib/foo, which contains a spec folder:
Rails.root
/lib
/foo
/lib
/spec
When I run rspec in Rails root, I would like the foo specs to be ran too. How can I accomplish it?
You could run it like that: rspec spec/ lib/foo/spec
I started making a Rails 3.1 engine, and I'm having a hard time testing it using rspec.
First of all, if I run rails g integration_test whatever it creates a regular integration test in tests/integration instead of spec/requests (the rspec-rails gem is installed and required as a development dependency in the gemspec file)
Also, when I run a spec test I get an error saying the table corresponding to the model I'm testing has not been created. I tried rake engine_name:install:migrations and running rake db:migrate from inside the dummy app, and I get a "table already exists" error.
Everything just seems disconnected, I feel I'm missing something here to make the rspec gem work seamlessly as it usually does with full rails applications.
I followed all the changes from here http://rubyx.com/2011/03/01/start-your-engines and I can test the engine manually by launching the dummy app via the console as shown here http://railscasts.com/episodes/277-mountable-engines.
Is there a way to make rspec the default for testing a rails 3.1 engine?
I am using RSpec with a Rails engine without issues.
I created my plugin using the following switches: -T --full --dummy-path=spec/dummy.
-T excludes test/unit
--full indicates that the plugin is an engine
--dummy-path is simply so that we don't get a test directory (the
default is test/dummy).
From there I used the spec_helper from the "start your engines" article:
# Configure Rails Envinronment
ENV["RAILS_ENV"] = "test"
require File.expand_path("../dummy/config/environment.rb", __FILE__)
require 'rspec/rails'
ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../')
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[File.join(ENGINE_RAILS_ROOT, "spec/support/**/*.rb")].each {|f| require f }
RSpec.configure do |config|
config.use_transactional_fixtures = true
end
For the generators. I add a config.generators block to my engine.rb file like so:
module MyEngine
class Engine < Rails::Engine
config.generators do |g|
g.test_framework :rspec, :view_specs => false
end
end
end
With that, I'm able to get rspec tests when running a generator like the model generator.
As for the DB, is your database.yml file set up correctly? Did you load the test environment, e.g. rake db:test:clone or rake db:migrate RAILS_ENV=test? My guess is that RSpec can't see your tables because there isn't a test database set up.
I was looking for the same answer and I found the combustion gem* which promise to setup a full environment for spec'ing your engine in a simpler way. Just add
gem.add_development_dependency 'combustion', '~> 0.3.1'
to your gemspec and run
bundle exec combust
to reproduce a full rails app in your spec directory.
*I haven't tried it yet...