I am new to ruby and Ruby on Rails. I used command rails new [project name] to create a new rails project, and when using rails generate scaffold [Model name] to create a model, rails itself always create a testing module related to the model and the controller. so, how do i remove all rails default testing module and replace with some custom testing modules like Rspec?
If you have gem rspec-rails in your Gemfile, and don't have minitest, then rails generators already informed that you use Rspec instead of Minitest. rails generate command will create specs instead of tests.
Off-topic: it creates fixtures as well. If instead of fixtures you use factories you need to declare it to your config-file:
# config/application.rb
config.generators do |g|
g.factory_bot dir: 'spec/factories'
end
Related
I'm using Ruby 2.2.0p0, and Rails 4.2.0. The Rails application is currently defaulting to using minitest, even though there's no testing gems mentioned in the Gemfile. For example, I have rake test in my rake tasks, and a test folder.
I can see how to install RSpec-rails. But what, if anything, do I need to do to uninstall minitest?
The only thing the RSpec-Rails README has is
Once installed, RSpec will generate spec files instead of Test::Unit
test files when commands like rails generate model and rails generate
controller are used.
It seems to me that in your case it not necessary to uninstall minitest, just add rspec to your Gemfile and start using it. However I recommend you to make RSpec as your default test framework.
Just add to config/application.rb next line:
config.generators.test_framework :rspec
That's it. Now Rails knows that you use RSpec and when you use Rails generators, it will add appropriate RSpec templete files, not Minitest ones. If you have test folder in your application (it's used by Minitest and Test::Unit) you can delete it now.
So I did a:
--skip-test-unit
When installing rails 4, and now I want to use testunit / minitest (not sure which one is default in rails 4.1?)
So how do I re-add testunit / minitest to rails?
There is no default rake command for this but the simplest workaround is generate new rails application with test
rails new test1
now copy the test folder from test1 to your repository
cp -r test ../your_repository_name
open config/application.rb
uncomment require "rails/test_unit/railtie"
now when you will generate model or controller in you application, then it will generate test files also.
For previous model and controller you can use
rails generate test_unit:controller ControllerName
rails generate test_unit:model ModelName
By default Rails 4.1 is using minitest (5.3.3)
I have a Rails application using Rails 3.
I added rspec-rails to my Gemfile:
group :development, :test do
gem 'rspec-rails'
end
and then I run bundle install. It shows my gem list, and all rspec gems are there (core, rails, etc.).
However, when I run
rails g rspec:install
that's what it returns:
create .rspec
create spec
create spec/spec_helper.rb
Although I have models and controllers in my application, it just create those files. Why isn't Rspec creating the spec files?
This is no longer true, and you can set the generator to create RSpec spec files when generating new parts of your rails application, as well as creating spec files for existing sections of the app.
The main feature lies in the application's generator configuration which is enabled when running the rspec-rails rspec:install task, but if you want to specify specific spec files to include/exclude, you may want this:
config/environments/development.rb // or any env you want
Rails.application.configure do
...
config.generators do |g|
g.test_framework :rspec
g.fixture_replacement :factory_bot
g.factory_bot dir: 'spec/factories'
g.controller_specs false
g.request_specs true
g.helper_specs false
g.feature_specs true
g.mailer_specs true
g.model_specs true
g.observer_specs false
g.routing_specs false
g.view_specs false
end
end
Generator Settings
The 'test_framework' option allows the rails to know exactly what test framework to create test files for, and generate new files based on your settings.
With the 'fixture_replacement', we can keep rails from generating fixtures by default, and instead create factories with each model created.
Lastly are the 'factory_bot' options, where you can change the factory folder default if needed, but will default to this directory on install. You can find more options in the Factory Girl/Bot Instructions.
Now when we generate something new, like a model:
> rails g model settings
invoke active_record
create db/migrate/20170915173537_create_settings.rb
create app/models/setting.rb
invoke rspec
create spec/models/setting_spec.rb
invoke factory_girl
create spec/factories/settings.rb
Generating Spec Files For Pre-Generated Sections of the App
Similar to generating rails files, you can generate spec files through Rspec's own task:
> rails g rspec:model old_settings
create spec/models/old_settings_spec.rb
invoke factory_girl
create spec/factories/old_settings.rb
This command uses the same conventions as the rails' generate command to create spec files, including scaffold, so you can create spec files for an entire namespace.
Rspec doesn't automatically create specs for your existing models and controllers. You'll have to go create those files yourself now.
Creating a spec file is really easy. Just make a file ending in _spec.rb and put this in it:
require 'spec_helper';
describe User do;
end
(of course replacing User with the class you are testing) and you're there.
As a footnote, if your generator command does not generate a file you expect, check the application.rb config file to make sure specific specs were not disabled.
For me following generator did not create file as expected.
% bin/rails g rspec:request api
Running via Spring preloader in process 70924
%
because in application.rb
g.request_specs false
was set. While this seems obvious after the fact, command output is not intuitive. Setting it to true and re-running the generator produces expected result.
% bin/rails g rspec:request api
Running via Spring preloader in process 71843
create spec/requests/apis_spec.rb
I apologize if this question is slightly subjective... I am trying to figure out the best way to test Rails 3 Engines with Cucumber & Rspec. In order to test the engine a rails 3 app is necessary. Here is what I am currently doing:
Add a rails test app to the root of the gem (myengine) by running: rails new /myengine/rails_app
Add Cucumber to /myengine/rails_app/features as you would in a normal Rails app
Require the Rails Engine Gem (using :path=>"/myengine") in /myengine/rails_app/Gemfile
Add spec to the root directory of the gem: /myengine/spec
Include the fixtures in /myengine/spec/fixtures and I add the following to my cuc env.rb:
env.rb:
Fixtures.reset_cache
fixtures_folder = File.join(Rails.root, 'spec', 'fixtures')
fixtures = Dir[File.join(fixtures_folder, '*.yml')].map {|f| File.basename(f, '.yml') }
Fixtures.create_fixtures(fixtures_folder, fixtures)
Do you see any problems with setting it up like this? The tests run fine, but I am a bit hesitant to put the features inside the test rails app. I originally tried putting the features in the root of the gem and I created the test rails app inside features/support, but for some reason my engine would not initialize when I ran the tests, even though I could see the app loading everything else when cuc ran.
If anyone is working with Rails Engines and is using cuc and rspec for testing, I would be interested to hear your setup.
**UPDATE
I changed my setup a bit since I wrote this question. I decided to get rid of the spec directory under the root of the engine. Now I just create a rails app named "test_app" and setup cuc and rspec inside that app like I would normally do in a rails app. Then I include the gem like I did in step #3 above. Since the engine is a sub-app, I guess its just best to test it like it was a normal rails app. I am still interested in hearing if anyone has a different setup.
Rails 3.1 (will) generate a pretty good scaffold for engines. I'd recommend using RVM to create a new gemset called edge and switch to it:
rvm gemset create edge
rvm use #edge
Then install edge rails:
git clone git://github.com/rails/rails.git
cd rails
rake install
From there, you can follow Piotr Sarnacki's mountable app tutorial, replacing calls such as:
bundle exec ./bin/rails plugin new ../blog --edge --mountable
With simply:
rails plugin new blog --mountable --full
The mountable option makes the application mountable, whilst the full option makes it an engine with tests already built-in. To test the engine, this generator generates a folder in test called dummy which contains a small Rails application. You can see how this is loaded in test/test_helper.rb.
Then it's up to you to massage the data to do what it needs to in order to work. I would recommend copying over the cucumber files from a standard rails g cucumber:install into the project and then messing about with it until it works. I've done this once before so I know it's possible, but I cannot find the code right now.
Let me know how you go.
I'll explain how I did it using as example the following gem: https://github.com/skozlov/netzke-core
The testing application. It is in netzke-core/test/rails_app. This app can be run independently, so I can also use it for manual testing or for playing around with new features if I like.
In order for the testing app to load the gem itself, I have the following in application.rb:
$:.unshift File.expand_path('../../../../lib', __FILE__)
require 'netzke-core'
Cucumber features. They are in netzke-core/features. In env.rb I have:
require File.expand_path(File.dirname(__FILE__) + '/../../test/rails_app/config/environment')
... which will load the testing application before executing the features.
Specs. These are in netzke-core/spec. In spec_helper.rb I have the following:
require File.expand_path("../../test/rails_app/config/environment", __FILE__)
... which will load the testing application before running the specs.
Running tests. This setup lets me run the tests from the root of the gem:
cucumber features
and
rspec spec
Factory Girl. Not for this particular gem, but I'm normally using factory_girl instead of fixtures (see, for example, a similar setup in https://github.com/skozlov/netzke-basepack).
A bit late to the party, but here is my strategy:
Generating the rails plugin in 3.2:
rails plugin new blog --mountable --full
This creates test/dummy, containing the dummy rails app
Add the specs to spec
Move the dummy folder to spec (and optionally get rid of the other testfiles)
Adapt specs/spec_helper.rb so it includes
require File.expand_path("../.../config/environment", __FILE__)
instead of
require File.expand_path("../dummy/config/environment", __FILE__)
Execute rails g cucumber:install. It will generate features folder a.o.
Add
ENV["RAILS_ROOT"] ||= File.expand_path(File.dirname(__FILE__) + '/../../spec/dummy')
before
require 'cucumber/rails'
in features/support/env.rb
Now you have features and spec in the root of you project, while the dummy rails app is neatly tucked away under spec/dummy
I am using RSpec, but Rails insists on generating Test::Unit tests. Maybe there is a configuration I missed?
if you are using rails 2 then you do:
ruby script/generate rspec_model Foo
using Rails 3 check out this example:
http://paulbarry.com/articles/2010/01/13/customizing-generators-in-rails-3
If you're using Rails 3, you need the rspec-rails integration gem from github: https://github.com/rspec/rspec-rails
The instructions are there on Github, but you will need to run
script/rails generate rspec:install
From the README:
"RSpec is registered with Rails as the test framework, so whenever you generate application components like models, controllers, etc, RSpec specs are generated instead of Test::Unit tests."