views:

104

answers:

2

I have one spec:

require 'spec_helper'

# hmm... I need to include it here because if I include it inside describe block `method_missing` exception raises.
include Shoulda::ActionController::Matchers

describe CategoriesController do
  include Devise::TestHelpers

  render_views

  context "new should render template :new" do
    setup do
      sign_in :user
      get :new
    end

    should render_template(:new)
  end
end

when I run rake spec I see this(but if i change context to it and move setup block contents to it block all works fine):

localhost:gallery smix$ rake spec
(in /Users/smix/Projects/gallery)
/Users/smix/.rvm/rubies/ruby-1.9.2-p0/bin/ruby -S bundle exec  /Users/smix/.rvm/rubies/ruby-1.9.2-p0/bin/ruby  -Ilib -Ispec "./spec/controllers/categories_controller_spec.rb" "./spec/controllers/photos_controller_spec.rb" "./spec/helpers/categories_helper_spec.rb" "./spec/helpers/photos_helper_spec.rb" "./spec/models/category_spec.rb" 
***

Pending:
  CategoriesHelper add some examples to (or delete) /Users/smix/Projects/gallery/spec/helpers/categories_helper_spec.rb
    # Not Yet Implemented
    # ./spec/helpers/categories_helper_spec.rb:14
  PhotosHelper add some examples to (or delete) /Users/smix/Projects/gallery/spec/helpers/photos_helper_spec.rb
    # Not Yet Implemented
    # ./spec/helpers/photos_helper_spec.rb:14
  Category add some examples to (or delete) /Users/smix/Projects/gallery/spec/models/category_spec.rb
    # Not Yet Implemented
    # ./spec/models/category_spec.rb:4

Finished in 0.0006 seconds
3 examples, 0 failures, 3 pending
/Users/smix/.rvm/gems/ruby-1.9.2-p0/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/render_template_matcher.rb:41:in `renders_template?': undefined method `assert_template' for #<Class:0x00000104839eb0> (NoMethodError)
        from /Users/smix/.rvm/gems/ruby-1.9.2-p0/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/render_template_matcher.rb:23:in `matches?'
        from /Users/smix/.rvm/gems/ruby-1.9.2-p0/gems/rspec-expectations-2.0.0.beta.20/lib/rspec/expectations/handler.rb:11:in `handle_matcher'
        from /Users/smix/.rvm/gems/ruby-1.9.2-p0/gems/rspec-expectations-2.0.0.beta.20/lib/rspec/expectations/extensions/kernel.rb:27:in `should'
        from ./spec/controllers/categories_controller_spec.rb:17:in `block (2 levels) in <main>'
        from /Users/smix/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/example_group.rb:129:in `module_eval'
        from /Users/smix/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/example_group.rb:129:in `subclass'
        from /Users/smix/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/example_group.rb:116:in `describe'
        from ./spec/controllers/categories_controller_spec.rb:11:in `block in <main>'
        from /Users/smix/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/example_group.rb:129:in `module_eval'
        from /Users/smix/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/example_group.rb:129:in `subclass'
        from /Users/smix/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/example_group.rb:116:in `describe'
        from /Users/smix/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/extensions/object.rb:7:in `describe'
        from ./spec/controllers/categories_controller_spec.rb:6:in `<main>'
Loaded suite /Users/smix/.rvm/gems/ruby-1.9.2-p0/bin/rake
Started

Finished in 0.003418 seconds.

0 tests, 0 assertions, 0 failures, 0 errors, 0 skips

Test run options: --seed 39993

My spec_helper.rb:

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'shoulda'
require 'shoulda/rails'
require 'shoulda/integrations/rspec2'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}


RSpec.configure do |config|
  # == Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr
  config.mock_with :rspec

  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, comment the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true
end

I am using rails 3. rspec/shoulda/factory_girl are included to the application by this code in Gemfile:

group :test, :development do
  gem 'autotest'
  gem 'factory_girl'
  gem "shoulda"
  gem "rspec-rails", ">= 2.0.0.beta.20"
end
A: 

If I'm not mistaken, I've used Shoulda and I think the syntax is

should_render_template(:new)

and not

should render_template(:new)

which explains the 0 tests,0 assertions...

Shreyas Satish
this don't work too. I get error 'undefined method `should_render_template''
SMiX
A: 

I'm pretty sure that error is because you haven't wrapped your shoulda assertion inside an it block like so:

it { should render_template(:new) }

This is required when using Shoulda with RSpec.

should render_template(:new) on it's own will work with Test::Unit.

Sidane
Yes but I need to run shoulda helpers inside `context` block. Is it possible?
SMiX
Yes, you can have as many assertions within a context block as you like. The CategoriesController spec that you posted is fine, just wrap `should render_template(:new)` in an `it` block and it should work.
Sidane