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)
Related
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
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.
I am reading ruby and rails tutorial 2nd edition book.
I am facing problem to do dynamic title in title pages.
In this book write done this path "spec/requests/static_pages_spec.rb" and i have not found any spec folder in rails project where it is located ? where can create a static_pages_spec.rb file ?
I am using rails 4.0.2
book link :http://ruby.railstutorial.org/chapters/rails-flavored-ruby#top
Listing 4.4. Updated tests for the Home page’s title.
spec/requests/static_pages_spec.rb
please help me
path "spec/requests/static_pages_spec.rb "
May i create it spec folder in rails root folder ?
Folder spec located in rails root dir.
in Rails you can use Rspec generator or create helper:
add to Gemfile:
gem 'rspec-rails'
run rails generator:
rails g rspec:install
This create folder spec in you project and generate spec_helper.rb
And run rspec or bundle exec rspec.
Folder spec located in rails root directory in your rails app.
in Rails you can use Rspec generator:
following Gem add to Gemfile:
gem 'rspec-rails'
and then run following command on terminal
rails g rspec:install
This create folder spec in you project and generate spec_helper.rb
And run rspec or bundle exec rspec
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."