-1

I ran into some trouble with section 6.2.1 (validity test) of Michael Hartl's "Rails Tutorial" and realized that I don't even have a test directory created as a result of

$ rails generate model User name:string email:string

While the tutorial says that I should see the output

 invoke  active_record
  create    db/migrate/20140724010738_create_users.rb
  create    app/models/user.rb
  invoke    test_unit
  create      test/models/user_test.rb
  create      test/fixtures/users.yml

all I am actually seeing is:

   invoke  active_record
   identical    db/migrate/20141020202519_create_users.rb
   identical    app/models/user.rb
   invoke    rspec
   identical      spec/models/user_spec.rb

I've researched tons of different sites looking for the answer and noticed people were suggesting to move rspec-rails around in the Gemfile but from what I can see, mine is placed properly. Here is my Gemfile:

source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0

gem 'rails', '4.0.8'
gem 'bootstrap-sass', '2.3.2.0'
gem 'sprockets', '2.11.0'

group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
gem 'guard-rspec', '2.5.0'
gem 'spork-rails', '4.0.0'
gem 'guard-spork', '1.5.0'
gem 'childprocess', '0.3.6'
end

group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
end

gem 'sass-rails', '4.0.3'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'

group :doc do
  gem 'sdoc', '0.3.20', require: false
end

group :production do
  gem 'pg', '0.15.1'
  gem 'rails_12factor', '0.0.2'
end

I would really appreciate any help! Thanks

  • This is only because you're using rspec rather than unit test - nothing to worry here, rspec is more common in use nowadays. – BroiSatse Oct 24 '14 at 16:09
  • From the output, it looks like the tutorial is using Test::Unit (propably using Minitest gem) as the testing framework, but you are using RSpec instead. make sure you are using the right gem and your proejct is setup correctly. – Alireza Oct 24 '14 at 16:09
  • Welcome to Stack Overflow. When you write a question, please use a more descriptive title, something that applies to the problem, not to what you're doing. Also, it really helps us if you strip the problem code down to the bare minimum necessary to replicate the problem. Anything beyond that wastes our time when trying to help you, and needlessly clouds the issue when other people search for an answer to the same question and find yours. – the Tin Man Oct 24 '14 at 16:19
  • well as you can see i'm new to programming in general so i don't have any way of knowing what the bare minimum necessary would be. sorry if this was too much information but it's the best i could do. – Kenneth Marks Oct 24 '14 at 18:19

3 Answers3

0

What is happening here is rspec-rails is overriding the default behavior of rails. Rails by default ships with test_unit (as shown by the invoke test_unit in original output).

Instead with rspec, you generate spec files instead. Thus you have invoke rspec and spec/models/user_spec.rb.

If you remove rspec-rails from your Gemfile and run the bundle install and then rails g model User name:string email:string again, it will generate the test files according to the tutorial.

Bibek Shrestha
  • 32,848
  • 7
  • 31
  • 34
0

If you want the same output, put the related rspec gem in the test group only.

group :development, :test do
    gem 'sqlite3', '1.3.8'
    gem 'spork-rails', '4.0.0'
    gem 'guard-spork', '1.5.0'
    gem 'childprocess', '0.3.6'
end

group :test do
    gem 'selenium-webdriver', '2.35.1'
    gem 'capybara', '2.1.0'
    gem 'rspec-rails', '2.13.1'
    gem 'guard-rspec', '2.5.0'
end

And the update:

bundle update

It worked for me for the inverse process (invoke rspec on generating the model) placing rspec gems into development and test groups.

If you don't want to use rspec as the test suite, just remove the gems from your gemfile.

rogelio
  • 1,541
  • 15
  • 19
0

It looks like the online version of the Rails Tutorial has recently undergone a major overhaul. Previously, the tutorial used rspec for testing; now it's using the test framework that comes with rails. If you look at section 3.1, the Gemfile for the sample app now looks like this:

source 'https://rubygems.org'

gem 'rails',                '4.2.0.beta2'
gem 'sass-rails',           '5.0.0.beta1'
gem 'uglifier',             '2.5.3'
gem 'coffee-rails',         '4.0.1'
gem 'jquery-rails',         '4.0.0.beta2'
gem 'turbolinks',           '2.3.0'
gem 'jbuilder',             '2.2.3'
gem 'rails-html-sanitizer', '1.0.1'
gem 'sdoc',                 '0.4.0', group: :doc

group :development, :test do
  gem 'sqlite3',     '1.3.9'
  gem 'byebug',      '3.4.0'
  gem 'web-console', '2.0.0.beta3'
  gem 'spring',      '1.1.3'
end

group :test do
  gem 'minitest-reporters', '1.0.5'
  gem 'mini_backtrace',     '0.1.3'
  gem 'guard-minitest',     '2.3.1'
end

group :production do
  gem 'pg',             '0.17.1'
  gem 'rails_12factor', '0.0.2'
end

And in section 3.3.1, the first test looks like this:

require 'test_helper'

class StaticPagesControllerTest < ActionController::TestCase

  test "should get home" do
    get :home
    assert_response :success
  end

  test "should get help" do
    get :help
    assert_response :success
  end
end

Because the tutorial is now using a different test framework, you will no longer get the same output as the tutorial. And in fact, you will no longer be able to follow along with the tests in the tutorial. Unfortunately, I think you will have to start over. I was on chapter 7, so I am in the same position as you. I emailed the author to see if he would be willing to put up the old version somewhere.

Michael Hartl got back to me. Here's a link to the old version:

http://rails-4-0.railstutorial.org/book

7stud
  • 46,922
  • 14
  • 101
  • 127