Giter Site home page Giter Site logo

rspec-rails's Introduction

rspec-rails Code Climate Gem Version

rspec-rails brings the RSpec testing framework to Ruby on Rails as a drop-in alternative to its default testing framework, Minitest.

In RSpec, tests are not just scripts that verify your application code. They’re also specifications (or specs, for short): detailed explanations of how the application is supposed to behave, expressed in plain English.

According to RSpec Rails new versioning strategy use:

Installation

IMPORTANT This README / branch refers to the current development build. See the 6-1-maintenance branch on Github if you want or require the latest stable release.

  1. Add rspec-rails to both the :development and :test groups of your app’s Gemfile:

    # Run against this stable release
    group :development, :test do
      gem 'rspec-rails', '~> 6.1.0'
    end
    
    # Or, run against the main branch
    # (requires main-branch versions of all related RSpec libraries)
    group :development, :test do
      %w[rspec-core rspec-expectations rspec-mocks rspec-rails rspec-support].each do |lib|
        gem lib, git: "https://github.com/rspec/#{lib}.git", branch: 'main'
      end
    end

    (Adding it to the :development group is not strictly necessary, but without it, generators and rake tasks must be preceded by RAILS_ENV=test.)

  2. Then, in your project directory:

    # Download and install
    $ bundle install
    
    # Generate boilerplate configuration files
    # (check the comments in each generated file for more information)
    $ rails generate rspec:install
          create  .rspec
          create  spec
          create  spec/spec_helper.rb
          create  spec/rails_helper.rb

Upgrading

If your project is already using an older version of rspec-rails, upgrade to the latest version with:

$ bundle update rspec-rails

RSpec follows semantic versioning, which means that “major version” upgrades (e.g., 2.x → 3.x) come with breaking changes. If you’re upgrading from version 2.x or below, read the rspec-rails upgrade notes to find out what to watch out for.

Be sure to check the general RSpec upgrade notes as well.

Usage

Creating boilerplate specs with rails generate

# RSpec hooks into built-in generators
$ rails generate model user
      invoke  active_record
      create    db/migrate/20181017040312_create_users.rb
      create    app/models/user.rb
      invoke    rspec
      create      spec/models/user_spec.rb

# RSpec also provides its own spec file generators
$ rails generate rspec:model user
      create  spec/models/user_spec.rb

# List all RSpec generators
$ rails generate --help | grep rspec

Running specs

# Default: Run all spec files (i.e., those matching spec/**/*_spec.rb)
$ bundle exec rspec

# Run all spec files in a single directory (recursively)
$ bundle exec rspec spec/models

# Run a single spec file
$ bundle exec rspec spec/controllers/accounts_controller_spec.rb

# Run a single example from a spec file (by line number)
$ bundle exec rspec spec/controllers/accounts_controller_spec.rb:8

# See all options for running specs
$ bundle exec rspec --help

Optional: If bundle exec rspec is too verbose for you, you can generate a binstub at bin/rspec and use that instead:

$ bundle binstubs rspec-core

RSpec DSL Basics (or, how do I write a spec?)

In RSpec, application behavior is described first in (almost) plain English, then again in test code, like so:

RSpec.describe 'Post' do           #
  context 'before publication' do  # (almost) plain English
    it 'cannot have comments' do   #
      expect { Post.create.comments.create! }.to raise_error(ActiveRecord::RecordInvalid)  # test code
    end
  end
end

Running rspec will execute this test code, and then use the plain-English descriptions to generate a report of where the application conforms to (or fails to meet) the spec:

$ rspec --format documentation spec/models/post_spec.rb

Post
  before publication
    cannot have comments

Failures:

  1) Post before publication cannot have comments
     Failure/Error: expect { Post.create.comments.create! }.to raise_error(ActiveRecord::RecordInvalid)
       expected ActiveRecord::RecordInvalid but nothing was raised
     # ./spec/models/post.rb:4:in `block (3 levels) in <top (required)>'

Finished in 0.00527 seconds (files took 0.29657 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/models/post_spec.rb:3 # Post before publication cannot have comments

For an in-depth look at the RSpec DSL, including lots of examples, read the official Cucumber documentation for RSpec Core.

Helpful Rails Matchers

In RSpec, assertions are called expectations, and every expectation is built around a matcher. When you expect(a).to eq(b), you’re using the eq matcher.

In addition to the matchers that come standard in RSpec, here are some extras that make it easier to test the various parts of a Rails system:

RSpec matcher Delegates to Available in Notes
be_a_new all primarily intended for controller specs
render_template assert_template request / controller / view use with expect(response).to
redirect_to assert_redirect request / controller use with expect(response).to
route_to assert_recognizes routing / controller use with expect(...).to route_to
be_routable routing / controller use with expect(...).not_to be_routable
have_http_status request / controller / feature
match_array all for comparing arrays of ActiveRecord objects
have_been_enqueued all requires config: ActiveJob::Base.queue_adapter = :test
have_enqueued_job all requires config: ActiveJob::Base.queue_adapter = :test

Follow the links above for examples of how each matcher is used.

What else does RSpec Rails add?

For a comprehensive look at RSpec Rails’ features, read the official Cucumber documentation.

What tests should I write?

RSpec Rails defines ten different types of specs for testing different parts of a typical Rails application. Each one inherits from one of Rails’ built-in TestCase classes, meaning the helper methods provided by default in Rails tests are available in RSpec, as well.

Spec type Corresponding Rails test class
model
controller ActionController::TestCase
mailer ActionMailer::TestCase
job
view ActionView::TestCase
routing
helper ActionView::TestCase
request ActionDispatch::IntegrationTest
feature
system ActionDispatch::SystemTestCase

Follow the links above to see examples of each spec type, or for official Rails API documentation on the given TestCase class.

Note: This is not a checklist.

Ask a hundred developers how to test an application, and you’ll get a hundred different answers.

RSpec Rails provides thoughtfully selected features to encourage good testing practices, but there’s no “right” way to do it. Ultimately, it’s up to you to decide how your test suite will be composed.

When creating a spec file, assign it a type in the top-level describe block, like so:

# spec/models/user_spec.rb

RSpec.describe User, type: :model do
...

System specs, feature specs, request specs–what’s the difference?

RSpec Rails provides some end-to-end (entire application) testing capability to specify the interaction with the client.

System specs

Also called acceptance tests, browser tests, or end-to-end tests, system specs test the application from the perspective of a human client. The test code walks through a user’s browser interactions,

  • visit '/login'
  • fill_in 'Name', with: 'jdoe'

and the expectations revolve around page content.

  • expect(page).to have_text('Welcome')

Because system specs are a wrapper around Rails’ built-in SystemTestCase, they’re only available on Rails 5.1+. (Feature specs serve the same purpose, but without this dependency.)

Feature specs

Before Rails introduced system testing facilities, feature specs were the only spec type for end-to-end testing. While the RSpec team now officially recommends system specs instead, feature specs are still fully supported, look basically identical, and work on older versions of Rails.

On the other hand, feature specs require non-trivial configuration to get some important features working, like JavaScript testing or making sure each test runs with a fresh DB state. With system specs, this configuration is provided out-of-the-box.

Like system specs, feature specs require the Capybara gem. Rails 5.1+ includes it by default as part of system tests, but if you don’t have the luxury of upgrading, be sure to add it to the :test group of your Gemfile first:

group :test do
  gem "capybara"
end

Request specs

Request specs are for testing the application from the perspective of a machine client. They begin with an HTTP request and end with the HTTP response, so they’re faster than feature specs, but do not examine your app’s UI or JavaScript.

Request specs provide a high-level alternative to controller specs. In fact, as of RSpec 3.5, both the Rails and RSpec teams discourage directly testing controllers in favor of functional tests like request specs.

When writing them, try to answer the question, “For a given HTTP request (verb + path + parameters), what HTTP response should the application return?”

Contributing

Once you’ve cloned the repo and set up the environment, you can run the specs and Cucumber features, or submit a pull request.

See Also

RSpec base libraries

Recommended third-party extensions

rspec-rails's People

Contributors

ahorek avatar alexrothenberg avatar alindeman avatar aried3r avatar benoittgt avatar bquorning avatar cupakromer avatar dchelimsky avatar drwl avatar fabn avatar javierjulio avatar jdax avatar jonrowe avatar justinko avatar kenzo-tanaka avatar klyonrad avatar morgoth avatar myronmarston avatar olleolleolle avatar petergoldstein avatar pirj avatar shanecav84 avatar soulcutter avatar tubbo avatar vivekmiyani avatar wbreeze avatar wincent avatar xaviershay avatar ydah avatar yujinakayama avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rspec-rails's Issues

Don't assume MyApp::Application is defined in initializer

I'm building several engines from individual apps for an in-house project, and I test them with rspec. When run as an engine, the MyApp::Application type is not loaded (and imho it shouldn't be, there's a separate MyApp::Engine that is loaded instead.)

To wit, rspec's generated initializer breaks in this situation, since MyApp::Application is unavailable. Appending "if defined? MyApp::Application" to the final "end" is enough to fix it.

Error in rspec-rails.gemspec

spec/lib/rspec/rails/transactional_database_support_spec.rb doesn't exist.

should be spec/rspec/rails/transactional_database_support_spec.rb instead

render in view spec defaults to "closest" subject

I am using RSpec-2 on a Rails 3 project.

I have a view spec similar to

describe "some_controller/index.html.haml" do
  it "renders some text" do
    render
    response.should contain "some text"
  end
  context "something specific" do
     it "renders something else" do
        render #breaks here
        response.should have_selector("a")
     end
  end
end

The second call to render breaks the spec because, by default it tries to render template "/something specific" or the description passed to context (describe). The render error is similar to:

Missing template /something specific with {:formats=>nil} in view path

I believe in rspec-1, rspec-rails-1.3.2 the second call to render would still, by default, render some_controller/index.html.haml.

Helper Specs

Helper specs get generated, but they are not really "helper specs" in that they offer no specific support yet for spec'ing helpers.

They need to:

  • provide a helper object which, in rspec-rails-1, is an instance of ActionView::Base
  • make sure the helper object has access to all of the rails helpers (link_to, url_for, etc)
  • make sure the helper object includes the helper being spec'd, so its methods can be accessed

Generate the gem

There's no rspec-rails.gemspec file in the repository (I guess you generate it manually).
The effet to that is we can't use bundler to get and use the edge version.

Could it be possible to add the gemspec file ?

Controller specs should automatically have type

When describing a controller it has no type, so Rspec.configuration.include(Foo, :type => :controller) doesn't work.
While it is possible to specify it by hand, it would be nice if controllers have a type automatically (or the manual type declaration is generated in the file).

Calling "cookies" in a controller test before the http action breaks

This should replicate the bug:

class SomeController < ApplicationController
  def test
    head :ok
  end
end

describe SomeController do
  it "should not blow up when setting a cookie" do
    cookies[:a] = "b"
    get :test
  end
end

This raises quite a cryptic error. I tracked down the problem to the interaction between the ActionDispatch::Integration::Runner and ControllerExampleGroupBehaviour:

def app 
  described_class.action(@_action).tap do |endpoint|
    def endpoint.routes
      Rails.application.routes
    end
  end
end

%w[get post put delete head].map do |method|
  eval <<-CODE
    def #{method}(*args)
      @_action = args.shift
      super '/', *args
    end
  CODE
end

When cookies gets called on the runner the integration_session gets set up with the app - but since @_action still hasn't been set the integration_session ends up with an app with no action. When post gets called @_action gets set, but the app has already been cached and the test request fails.

assigns don't work on controller spec

on UserController.rb,i write following code :
@user = User.new

but on UserController spec file
assigns[:user].should_not be_nil # failed!!

assigns[:user] return nil alway.

View specs: response.should render_template fails

Hello there.

The following view spec produces this exception: "undefined method `assert_template' for #Rspec::Matchers::Matcher:0x482a600"

describe 'posts/show' do
  it "renders the article" do
    render
    response.should render_template('posts/_article')
  end
end

response returns an ActiveSupport::SafeBuffer object, is it normal?

I'm on the master branch, latest commit.

Request specs don't render templates

I've updated to recent (master) version of rspec-rails and noticed that request specs don't render templates anymore ("RSpec-generated template" text instead).
I've reverted to ref 38c04ca and it works there so it looks like recent changes to rendering broke things.

generated spec_helper.rb typo (missing '=')

./lib/generators/rspec/install/templates/spec/spec_helper.rb:

generates this:

config.use_transactional_examples false

But should generate this:

config.use_transactional_examples = false

Unable to startup autospec due to an error in the rspec-rails gem

Hi,

When I try to start up autospec by running:

AUTOFEATURE=true autospec

Is resulting in:

/opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/gems/1.8/gems/rspec-rails-1.3.2/lib/autotest/rails_rspec.rb:36: undefined method `singularize' for "permissions":String (NoMethodError)

I tried to install various versions of the gem, rspec and ZenTest and in different combinations but I keep on getting those errors.

Is this something I'm doing wrong?
To me it seems Rails' inflections are not loaded at the time rspec-rails needs them...

Wout

provide alternative to "test/unit/assertionfailederror" on ruby1.9.1

It looks like this file does not exist in the standard library of ruby1.9.1..

/home/mbj/.gem/ruby/1.9.1/gems/activesupport-3.0.0.beta/lib/active_support/dependencies.rb:169:in require': no such file to load -- test/unit/assertionfailederror (LoadError) from /home/mbj/.gem/ruby/1.9.1/gems/activesupport-3.0.0.beta/lib/active_support/dependencies.rb:169:inrequire'
from /home/mbj/.bundle/ruby/1.9.1/gems/rspec-rails-2.0.0.a10/lib/rspec/rails/matchers.rb:2:in <top (required)>' from /home/mbj/.gem/ruby/1.9.1/gems/activesupport-3.0.0.beta/lib/active_support/dependencies.rb:169:inrequire'
from /home/mbj/.gem/ruby/1.9.1/gems/activesupport-3.0.0.beta/lib/active_support/dependencies.rb:169:in require' from /home/mbj/.bundle/ruby/1.9.1/gems/rspec-rails-2.0.0.a10/lib/rspec/rails.rb:2:in<top (required)>'
from /home/mbj/.gem/ruby/1.9.1/gems/activesupport-3.0.0.beta/lib/active_support/dependencies.rb:169:in require' from /home/mbj/.gem/ruby/1.9.1/gems/activesupport-3.0.0.beta/lib/active_support/dependencies.rb:169:inrequire'
from /home/mbj/devel/testapp/vehicles/spec/spec_helper.rb:5:in <top (required)>' from ./spec/controllers/vehicles_controller_spec.rb:1:inrequire'
from ./spec/controllers/vehicles_controller_spec.rb:1:in `

'

controller method misses from controller specs

Since rspec-rails 2.0.0.beta.8 there is no method called "controller". The instance variable is still in place. This might be consistent with test/unit (not sure), but is not consistent with previous versions of rspec.

Not working generator (class_nesting_depth).

Using Ruby 1.9.1, Rails Edge, RSpec "Edge", RSpec-Rails "Edge". Generators configuration in application.rb

config.generators do |g|
  g.test_framework :rspec, :fixture => true, :views => false
  g.fixture_replacement :factory_girl, :dir => "spec/factories"
end

When trying to generate scaffolds RSpec causes exception:

$ rails generate scaffold foo
      invoke  active_record
      create    db/migrate/20100417161944_create_foos.rb
      create    app/models/foo.rb
      invoke    rspec
      create      spec/models/foo_spec.rb
(erb):1:in `template': undefined local variable or method `class_nesting_depth' for # (NameError)
    from /Users/alfanick/.rvm/rubies/ruby-1.9.1-p378/lib/ruby/1.9.1/erb.rb:753:in `eval'
    from /Users/alfanick/.rvm/rubies/ruby-1.9.1-p378/lib/ruby/1.9.1/erb.rb:753:in `result'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/actions/file_manipulation.rb:83:in `block in template'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/actions/create_file.rb:52:in `call'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/actions/create_file.rb:52:in `render'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/actions/create_file.rb:61:in `block (2 levels) in invoke!'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/actions/create_file.rb:61:in `open'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/actions/create_file.rb:61:in `block in invoke!'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/actions/empty_directory.rb:114:in `call'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/actions/empty_directory.rb:114:in `invoke_with_conflict_check'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/actions/create_file.rb:59:in `invoke!'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/actions.rb:86:in `action'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/actions/create_file.rb:24:in `create_file'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/actions/file_manipulation.rb:82:in `template'
    from /Volumes/WD Data/Projekty/Websites/photographers_catalog/lib/generators/rspec/model/model_generator.rb:10:in `create_test_file'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/task.rb:33:in `run'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/invocation.rb:109:in `block in invoke'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/invocation.rb:118:in `each'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/invocation.rb:118:in `map'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/invocation.rb:118:in `invoke'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/group.rb:267:in `block in _invoke_for_class_method'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/shell.rb:69:in `with_padding'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/group.rb:256:in `_invoke_for_class_method'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/group.rb:165:in `_invoke_from_option_test_framework'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/task.rb:33:in `run'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/invocation.rb:109:in `block in invoke'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/invocation.rb:118:in `each'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/invocation.rb:118:in `map'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/invocation.rb:118:in `invoke'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/group.rb:267:in `block in _invoke_for_class_method'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/shell.rb:69:in `with_padding'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/group.rb:256:in `_invoke_for_class_method'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/group.rb:165:in `_invoke_from_option_orm'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/task.rb:33:in `run'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/invocation.rb:109:in `block in invoke'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/invocation.rb:118:in `each'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/invocation.rb:118:in `map'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/invocation.rb:118:in `invoke'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/group.rb:36:in `block in start'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/base.rb:378:in `start'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/gems/thor-0.13.4/lib/thor/group.rb:29:in `start'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/railties/lib/rails/generators.rb:163:in `invoke'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/railties/lib/rails/commands/generate.rb:9:in `'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/dependencies.rb:209:in `require'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/dependencies.rb:209:in `block in require'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/dependencies.rb:195:in `block in load_dependency'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/dependencies.rb:523:in `new_constants_in'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/dependencies.rb:195:in `load_dependency'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/dependencies.rb:209:in `require'
    from /Users/alfanick/.rvm/gems/ruby-1.9.1-p378/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/railties/lib/rails/commands.rb:30:in `'
    from script/rails:9:in `require'
    from script/rails:9:in `'

It's all about some class_nesting_depth, but I can't find anything useful online. Looks like a bug in lib/generators/rspec/model/model_generator.rb

Routing specs

Hello,

I'm trying to write some routing specs for my controller.
As you're doing in the routing generator : http://github.com/rspec/rspec-rails/blob/master/lib/generators/rspec/scaffold/templates/routing_spec.rb
I've creates spec/routing/controller_routing_spec.rb with my routing specs.

However I constantly get the two following errors :

undefined method `params_from' for #<Rspec::Core::ExampleGroup::Nested_5::Nested_6:0x10a07ae80>

undefined method `route_for' for #<Rspec::Core::ExampleGroup::Nested_5::Nested_6:0x10a07ae80>

I guess that's because routing specing isn't implemented yet. So I create this ticket as a "todo".

Stubbing rails controller method causes failure on controller action call

Trying to stub a controller method throws an error when the controller action is called, e.g.:

controller.stub!(:current_host).and_return(Factory.build(:host))
post :create

Results in:

Failure/Error: post :create
can't convert nil into String
# (eval):3:in `post'

Removing the stub doesn't cause the error and the post is fails as expected. In fact even trying to dump the controller before the post causes this error (as I wasn't sure if it was still controller or had
been moved to an instance variable).

NoSQL store adapters

Allow using non active_record or non sql/relational databases, such as document or key-value stores. Have an adapter API with a sample Adapter implementation fx for Mongo DB :)

gemspec removed from git

Why was this removed from git? You can no longer use bundle install in Rails 3 to grab the latest version of rspec-rails.

render_template error

undefined method `keys' for nil:NilClass is raised when using render_template:

class PostsController < ApplicationController
  def show
    render :template => "posts/details"
  end
end

describe PostsController do
  it "should render the correct template" do
    get :show
    response.should render_template("posts/details")
  end
end
  • gem "rspec-rails", "2.0.0.beta.6"
  • gem "rails", "3.0.0.beta3"

Should use Rails module instead of constants

In the generated Rakefile and spec_helper, It should use Rails.root as it is meant to be used:

rspec.rake:

-spec_prereq = File.exist?(File.join(Rails.root, 'config', 'database.yml')) ? "db:test:prepare" : :noop
+spec_prereq = Rails.root.join('config', 'database.yml').exist? ? "db:test:prepare" : :noop

spec_helper.rb:

-require File.dirname(FILE) + "/../config/environment" unless defined?(RAILS_ROOT)
+require File.dirname(FILE) + "/../config/environment" unless defined?(Rails)

as_new_record not working?

Hi!

post = mock_model(Post).as_new_record
post.new_record?.should be_true # fails

Is it implemented yet?

undefined method `root' for Spec::Rails:Module

I've recently tried ruby 1.9.1 with my Rails apps. RSpec breaks with:

undefined method `root' for Spec::Rails:Module

This happens when I use things like Rails.root.join('spec', 'fixtures', 'bla.jpg') or Rails.env.production?

Obviously when I prefix Rails with :: the error goes away. On Ruby 1.8.7 the error doesn't exist.

controller specs: params hash does not contain :action or :controller

I'm using CanCan for authorisation and it relies on params[:action] and params[:controller] to determine what resource to load for authorisation checks in a before filter. The specs work when CanCan is disabled so the routing etc is all correct.

The spec is:

describe "GET 'new'" do
  it "should be successful" do
    get 'new'
    response.should be_success
  end
end

I can see that the params hash is empty within the action method which is wrong as it should have the action and controller entries. I can work around this by modifying the spec for now, i.e.:
describe "GET 'new'" do
it "should be successful" do
get 'new', { :action => :new, :controller => :users }
response.should be_success
end
end

I suspect this is due to the usage of ActionDispatch::Integration as you mention on similar issues (http://rspec.lighthouseapp.com/projects/5645/tickets/963-request-is-nil and http://github.com/rspec/rspec-rails/issues#issue/10).

View specs: render :locals => {...}

Hi!

I'd like to spec a partial and I need to pass local variables to it. The render method in view specs does not allow any parameters, as it was the case with Rspec1. Is there another way to set locals for the view or is it currently missing from Rspec2?

Thanks!

Julien.

rspec-rails requires ActiveRecord regardless of whether the rails app is using it

I am using MongoMapper with Rails 3. My application specifically doesn't include active record. My application.rb looks like this

/config/application.rb
   3 %w(
   4   action_controller
   5   action_mailer
   6   active_resource
   7   rails/test_unit
   8 ).each do |framework|
   9   require "#{framework}/railtie"
  10 end

When I debug through the app, ActiveRecord is not loaded the line before "require 'rspec/rails'". However, it is directly after.

Support view isolation in controller specs

Hi!

I read in the README file that view isolation is not implemented in controller specs. I'm missing it and thought it might be of some use to share that need here.

running a focused test with the 'spec' command doesn't work if the line_number is not the first line of an example or example group

here's the command i'm running, with the results summary next to each line

bundle exec spec /path/to/some_spec.rb --line_number=n

1.  require 'spec_helper'             # 0 examples, 0 failures
2.                                    # 0 examples, 0 failures
3.  describe 'something or other' do  # 2 examples, 1 failures
4.    it 'should pass' do             # 1 example,  0 failures
5.      true.should be_true           # 0 examples, 0 failures
6.    end                             # 0 examples, 0 failures
7.                                    # 0 examples, 0 failures
8.    it 'should fail' do             # 1 example,  1 failures
9.      fail                          # 0 examples, 0 failures
10.   end                             # 0 examples, 0 failures
11. end                               # 0 examples, 0 failures
12.                                   # 0 examples, 0 failures
  • rspec (2.0.0.beta.5)
  • rspec-core (2.0.0.beta.5)
  • rspec-expectations (2.0.0.beta.5)
  • rspec-mocks (2.0.0.beta.5)
  • rspec-rails (2.0.0.beta.5)

Autotest fails if test/ and spec/ folders are simultaneously in rails3 project

If I have spec/ (with real specs) and test/ (with scaffold tests) in rails project tree I got an error:

�[f/usr/lib/ruby/gems/svn/gems/rspec-rails-2.0.0.beta.8/lib/autotest/rails_rspec2.rb:36:in block (2 levels) in <top (required)>': undefined methodsingularize' for "companies":String (NoMethodError)
from /usr/lib/ruby/gems/svn/gems/autotest-4.2.10/lib/autotest.rb:529:in call' from /usr/lib/ruby/gems/svn/gems/autotest-4.2.10/lib/autotest.rb:529:intest_files_for'
from /usr/lib/ruby/gems/svn/gems/autotest-4.2.10/lib/autotest.rb:401:in block in find_files_to_test' from /usr/lib/ruby/gems/svn/gems/autotest-4.2.10/lib/autotest.rb:401:ineach'
from /usr/lib/ruby/gems/svn/gems/autotest-4.2.10/lib/autotest.rb:401:in map' from /usr/lib/ruby/gems/svn/gems/autotest-4.2.10/lib/autotest.rb:401:infind_files_to_test'
from /usr/lib/ruby/gems/svn/gems/autotest-4.2.10/lib/autotest.rb:256:in run_tests' from /usr/lib/ruby/gems/svn/gems/autotest-4.2.10/lib/autotest.rb:245:inget_to_green'
from /usr/lib/ruby/gems/svn/gems/cucumber-0.7.2/lib/autotest/cucumber_mixin.rb:54:in get_to_green' from /usr/lib/ruby/gems/svn/gems/cucumber-0.7.2/lib/autotest/cucumber_mixin.rb:28:inblock in run'
from /usr/lib/ruby/gems/svn/gems/cucumber-0.7.2/lib/autotest/cucumber_mixin.rb:26:in loop' from /usr/lib/ruby/gems/svn/gems/cucumber-0.7.2/lib/autotest/cucumber_mixin.rb:26:inrun'
from /usr/lib/ruby/gems/svn/gems/autotest-4.2.10/lib/autotest.rb:135:in run' from /usr/lib/ruby/gems/svn/gems/autotest-4.2.10/bin/autotest:54:in<top (required)>'
from /usr/bin/autotest:19:in load' from /usr/bin/autotest:19:in'

But if I move test/ to test2/ all is ok.

My system settings:
Arch Linux x86_64
ruby 1.9.3dev (2010-05-08 trunk 27674) x86_64-linux
Rails 3.0.0.beta.3

# gem-svn list | grep rspec
rspec (2.0.0.beta.8)
rspec-core (2.0.0.beta.8)
rspec-expectations (2.0.0.beta.8)
rspec-mocks (2.0.0.beta.8)
rspec-rails (2.0.0.beta.8)

gem-svn list | grep autotest

autotest (4.2.10)
autotest-growl (0.2.4)
autotest-rails-pure (4.1.0)

Gemfile:


source 'http://rubygems.org'
gem 'rails', '3.0.0.beta3'
gem 'devise', '1.1.rc1'
gem 'mysql'
group :test do
gem 'database_cleaner'
gem 'rspec-rails', '>=2.0.0.beta.8'
gem 'rspec', '>=2.0.0.beta.8'

gem 'test-unit'

gem 'cucumber', '0.7.2'
gem 'cucumber-rails', '0.3.1'#, :git => 'git://github.com/aslakhellesoy/cucumber-rails.git'
gem 'capybara'

gem 'autotest'

gem 'autotest-rails-pure'
gem 'autotest-growl'
gem 'machinist'
end

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.