user1185081
user1185081

Reputation: 2118

Understanding RSpec testing

I've been searching around for a while, and did not find a clear explanation about testing structure in RoR. (I'm learning with Michael Hartl's book).

  1. Since I'm using rspec for testing, do I need to keep the "test" folder anymore ?
  2. Is the "spec" folder structure strictly assigned to specific test purposes ?
  3. When "generating" a test script, does it do something else than creating the script file ? (i.e. could I create it by hand ?)

The 2nd question comes from this error:

undefined method `visit' for #<RSpec::Core::ExampleGroup: ...

which occurs as soon as my very simple test file is not in /spec/requests (but in /spec/views):

require 'spec_helper'

describe "Home page" do

  subject { page }

###1
  describe "title" do
    before { visit root_path }

    it { should have_selector('h1',    text: 'Welcome') }
    it { should have_selector('title', text: 'Home') }

  end
end

Upvotes: 0

Views: 64

Answers (3)

varatis
varatis

Reputation: 14750

1) No, you don't need the test folder. Only spec, unless you're using Test::Unit.

2) You can order spec however you want. The general suggested format is

spec/
  controllers/
  views/      
  models/ 
  lib/ #Only if you have stuff in lib you need to test    
  spec_helper.rb

with names like spec/controllers/items_controller_spec.rb, and spec/models/item_spec.rb

3) I'm not sure what you mean. For RSpec, you just need to make a file that uses its syntax to test certain pieces of your Ruby code. It doesn't create a script -- you do.

For your last question about visit:

It seems like you're trying to use a Capybara method (visit). Capybara takes care of visiting your site in a browser, not RSpec.

Upvotes: 2

Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

  1. No
  2. See Below
  3. No, only create the file

There is one specific folder structure you need to use in order to run Capybara acceptance tests. These are tests that have "visit" in them

Capybara 1.0

spec/requests

Capybara 2.0

spec/features

Upvotes: 1

Jason Swett
Jason Swett

Reputation: 45164

  1. No, my understanding is that the test folder is generally used for Test::Unit stuff. You can delete or keep it; doesn't hurt to leave it.
  2. There's a pretty specific way they want you to organize that stuff. If you have app/models/user.rb, for example, your spec should be in spec/models/user_spec.rb.
  3. I don't know of anything else it does besides creating the test script. You can totally create those by hand, and I do all the time.

Upvotes: 1

Related Questions