应该帮手不行

时间:2010-09-12 02:09:12

标签: ruby-on-rails testing rspec shoulda

我有一个规格:

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

当我运行rake spec时,我看到了这一点(但是如果我更改了上下文并将setup块内容移动到it阻止所有工作正常):

localhost:gallery rtest$ rake spec
(in /Users/rtest/Projects/gallery)
/Users/rtest/.rvm/rubies/ruby-1.9.2-p0/bin/ruby -S bundle exec  /Users/rtest/.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/rtest/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/rtest/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/rtest/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/rtest/.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/rtest/.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/rtest/.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/rtest/.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/rtest/.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/rtest/.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/rtest/.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/rtest/.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/rtest/.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/rtest/.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/rtest/.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/rtest/.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

我的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

我正在使用rails 3.通过此代码在Gemfile中将rspec / shoulda / factory_girl包含在应用程序中:

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

2 个答案:

答案 0 :(得分:3)

我很确定这个错误是因为你没有把你的shoulda断言包装在it块中,如下所示:

it { should render_template(:new) }

使用带有RSpec的Shoulda时需要这样做。

should render_template(:new)就可以使用Test :: Unit。

答案 1 :(得分:0)

如果我没弄错的话,我使用了Shoulda,我认为语法是

should_render_template(:new)

而不是

should render_template(:new)

解释了0个测试,0个断言......