0

我有一个规格:

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. rspec/shoulda/factory_girl 通过 Gemfile 中的此代码包含在应用程序中:

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

2 回答 2

3

我很确定这个错误是因为你没有it像这样将你的 shoulda 断言包装在一个块中:

it { should render_template(:new) }

在将 Shoulda 与 RSpec 一起使用时,这是必需的。

should render_template(:new)它自己将与 Test::Unit 一起使用。

于 2010-09-17T10:25:05.483 回答
0

如果我没记错的话,我用过Shouda,我认为语法是

should_render_template(:new)

并不是

should render_template(:new)

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

于 2010-09-12T06:45:10.693 回答