I am developing a Rails engine that depends on the Devise Gem. My Engine itself is a gem too and i need to write tests for that thing. For this i did lots of readings of how to test an engine from inside of a gem and i came to the following setup:
my_engine
+- app # my engine stuff
+- config # my engine stuff
+- Gemfile
+- lib # my engine stuff
+- public # my engine stuff
+- spec
+- controllers # controller tests
+- models # model tests
+- dummy # a dummy application that is using the 'my_engine/Gemfile'
+- spec_helper.rb # the spec helper that boots the dummy app
Before i included the Devise gem, i could write tests and run them with
rake spec
Now i have the Devise gem and a user model like this
class Manager::User < ActiveRecord::Base
devise :database_authenticatable, :registerable, # ...
end
and my tests fail pointing to the user class and the devise call.
`method_missing': undefined method `devise'
So now i try to investigate the error and find out how to boot up the dummy application with devise and my engine together. The spec helper looks like this
# my_engine/spec/spec_helper.rb#
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../dummy/config/environment.rb", __FILE__) # boots the dummy app and fails
# more rspec stuff gies here
The evnironment.rb
# my_engine/spec/dummy/config/environment.rb
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
Dummy::Application.initialize!
The application.rb
# my_engine/spec/dummy/config/application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(:default, Rails.env) if defined?(Bundler)
# Dummy::Application definition follows here (the common stuff)
The boot.rb
# my_engine/spec/dummy/config/boot.rb
require 'rubygems'
# Loads the gemfile from 'my_engine/Gemfile'
gemfile = File.expand_path('../../../../Gemfile', __FILE__)
begin
ENV['BUNDLE_GEMFILE'] = gemfile
require 'bundler'
Bundler.setup
rescue Bundler::GemNotFound => e
STDERR.puts e.message
STDERR.puts "Try running `bundle install`."
exit!
end if File.exist?(gemfile)
The gemfile
# my_engine/Gemfile
source :gemcutter
gem 'rails', '3.0.3'
gem 'rake'
gem 'devise', '>=1.2.rc'
gem 'declarative_authorization', '0.5.2'
group :test do
gem "cucumber"
gem "cucumber-rails"
gem "database_cleaner"
gem "capybara", ">= 0.4.0"
gem "capybara-envjs"
gem "culerity"
gem "webrat"
gem "rspec-rails", ">= 2.4.0"
gem "jeweler"
end
I tried also to require my engine like this
gem "my_engine", :path => "./"
The Gemfile and the gems are all loaded (Bundler.setup runs through and Devise constant is available).
Now i created an external application that uses my_engine gem. When i copy the tests from my engine and run them, then they all pass.
Is there anything I am missing? Maybe there is a better way to write tests for an engine?
To run a single Rspec test file, you can do: rspec spec/models/your_spec. rb to run the tests in the your_spec. rb file.
RSpec is a Behavior-Driven Development tool for Ruby programmers. BDD is an approach to software development that combines Test-Driven Development, Domain Driven Design and Acceptance Test-Driven Planning. RSpec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD.
RSpec is a unit test framework for the Ruby programming language. RSpec is different than traditional xUnit frameworks like JUnit because RSpec is a Behavior driven development tool. What this means is that, tests written in RSpec focus on the "behavior" of an application being tested.
What a beastly shame, i forgot to add the devise initializer into the dummy app. Somehow i thought about that for my external app. With the initializer it all works perfectly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With