为什么 RSpec 的方法“get"、“post"、“put"、“delete"不可用?在 gem(或 Rails 之外)的控制器规范中工作? [英] Why don't RSpec's methods, "get", "post", "put", "delete" work in a controller spec in a gem (or outside Rails)?

查看:16
本文介绍了为什么 RSpec 的方法“get"、“post"、“put"、“delete"不可用?在 gem(或 Rails 之外)的控制器规范中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是 Rails 或 Rspec 的新手,但我是制作 gem 的新手.当我测试我的控制器时,REST 方法get"、post"、put"、delete"给我一个未定义的方法错误.

I'm not new to Rails or Rspec, but I'm new to making gems. When I test my controllers, the REST methods "get", "post", "put", "delete" give me an undefined method error.

您会在下方找到代码,但如果您更喜欢在粘贴中看到它,请点击此处.

Below you'll find code, but if you prefer to see it in a pastie, click here.

谢谢!

这是我的 spec_helper:

Here's my spec_helper:


$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'rubygems'
require 'active_support' unless defined? ActiveSupport # Need this so that mattr_accessor will work in Subscriber module
require 'active_record/acts/subscribable'
require 'active_record/acts/subscriber'
require 'action_view'
require 'action_controller' # Since we'll be testing subscriptions controller
#require 'action_controller/test_process'
require 'spec'
require 'spec/autorun'


# Need active_support to user mattr_accessor in Subscriber module, and to set the following inflection
ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'dorkus', 'dorkuses'
end                     

require 'active_record' # Since we'll be testing a User model which will be available in the app

# Tell active record to load the subscribable files
ActiveRecord::Base.send(:include, ActiveRecord::Acts::Subscribable)
ActiveRecord::Base.send(:include, ActiveRecord::Acts::Subscriber)


require 'app/models/user' # The user model we expect in the application
require 'app/models/person'
require 'app/models/subscription'
require 'app/models/dorkus'
require 'app/controllers/subscriptions_controller' # The controller we're testing
#... more but I think irrelevant

我的订阅_规范:


require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe SubscriptionsController, "on GET index" do
  load_schema

  describe ", when only subscribable params are passed" do
    it "should list all the subscriptions of the subscribable object"
  end

  describe ", when only subscriber params are passed" do
    it "should list all the subscriptions of the subscriber" do
      u = User.create
      d1 = Dorkus.create
      d2 = Dorkus.create
      d1.subscribe! u
      d2.subscribe! u

      get :index, {:subscriber_type => "User", :subscriber_id => u.id}
      assigns[:subscriptions].should == u.subscriptions
    end
  end
end

我的订阅控制器:


class SubscriptionsController < ActionController::Base
  def index
  end
end

错误:


NoMethodError in 'SubscriptionsController on GET index , when only subscriber params are passed should list all the subscriptions of the subscriber'
undefined method `get' for #
/home/ramon/rails/acts_as_subscribable/spec/controllers/subscriptions_controller_spec.rb:21:

推荐答案

因为这些方法不属于 Rspec,而是属于 Rails.

Because these methods don't belong to Rspec but to Rails.

当你描述一个控制器时,它继承自 Spec::Rails::Example::ControllerExampleGroup,它继承了 FunctionalExampleGroup,它继承了 rails 的 ActionController::TestCase.

When you describe a controller, it inherits from Spec::Rails::Example::ControllerExampleGroup, which inherits FunctionalExampleGroup, which inherits the rails' ActionController::TestCase.

如果您查看 ActionController::TestCase 的文档,您会发现其中定义了 get/post/put/delete 方法.

If you look at ActionController::TestCase's documentation you'll find that's where the get/post/put/delete methods are defined.

所以如果你想在 Rails 之外访问这些方法,你需要重新定义它们.

So if you want to get access to these methods outside of Rails, you need to redefine them.

这篇关于为什么 RSpec 的方法“get"、“post"、“put"、“delete"不可用?在 gem(或 Rails 之外)的控制器规范中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆