gpt4 book ai didi

ruby-on-rails - 应该 helper 不工作

转载 作者:行者123 更新时间:2023-11-28 21:06:20 27 4
gpt4 key购买 nike

我有一个规范:

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 block 内容移动到 it block 所有工作很好):

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

最佳答案

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

it { should render_template(:new) }

在将 Shoulda 与 RSpec 结合使用时这是必需的。

应该 render_template(:new) 本身将与 Test::Unit 一起工作。

关于ruby-on-rails - 应该 helper 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3693463/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com