为什么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)?

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

问题描述

我对Rails或Rspec并不陌生,但我是新手制造宝石。当我测试我的控制器时,REST方法get,post,put,delete给了我一个未定义的方法错误。

找到代码,但如果你喜欢在一个pastie中看到它,请点击这里



谢谢!

这是我的spec_helper:

  
$ LOAD_PATH.unshift(File.dirname(__ FILE__))
$ LOAD_PATH.unshift(File.join(File.dirname(__ FILE__),'..','lib'))
需要'rubygems'
需要'active_support',除非定义? ActiveSupport#需要这个,以便mattr_accessor在Subscriber模块中工作
require'active_record / acts / subscribable'
require'active_record / acts / subscriber'
require'action_view'
require' action_controller'#因为我们将测试订阅控制器
#require'action_controller / test_process'
require'spec'
require'spec / autorun'


#需要active_support用户模块中的用户mattr_accessor,并设置下面的变化
ActiveSupport :: Inflector.inflections do | inflect |
inflect.irregular'dorkus','dorkuses'
end

require'active_record'#因为我们将测试用户模型,该模型将在应用程序$ b中提供
$ b#指示活动记录加载可订阅文件
ActiveRecord :: Base.send(:include,ActiveRecord :: Acts :: Subscribable)
ActiveRecord :: Base.send(:include ,ActiveRecord :: Acts :: Subscriber)


需要'app / models / user'#我们期望在应用程序
中的用户模型需要'app / models / person'
需要'app / models / subscription'
需要'app / models / dorkus'
需要'app / controllers / subscriptions_controller'#我们正在测试的控制器
#.. 。更多,但我认为不相关的

我的订阅商品规格:

  
需要File.expand_path(File.dirname(__ FILE__)+'/../spec_helper')

描述SubscriptionsController,on GET索引do
load_schema

描述,当只有可订阅的参数被传递时,do
it应该列出所有订阅对象
end

describe的所有订阅,当只有订阅者参数通过时do
it应列出用户的所有订阅do
u = User.create
d1 = Dorkus.create
d2 = Dorkus.create
d1。订阅! u
d2.subscribe! u

get:index,{:subscriber_type =>User,:subscriber_id => u.id}
赋值[:subscriptions] .should == u.subscriptions
结束
结束
结束

我的订阅控制器:

  
class SubscriptionsController< ActionController :: Base
def index
end
end

error':

  
'订阅控制器在GET索引中的NoMethodError,当只有订阅者参数传递时,应该列出所有的订阅订户的
未定义的方法`get'为#
/home/ramon/rails/acts_as_subscribable/spec/controllers/subscriptions_controller_spec.rb:21:

解决方案

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



当您描述控制器时,它从 Spec :: Rails :: Example :: ControllerExampleGroup ,它继承 FunctionalExampleGroup ,它继承了rails 'ActionController :: TestCase。



如果您看 ActionController :: TestCase的文档,你会发现这是定义get / post / put / delete方法的地方。所以如果你想要在Rails之外访问这些方法,您需要重新定义它们。

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.

Thanks!

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

My subscriptions_spec:


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

My subscriptions controller:


class SubscriptionsController < ActionController::Base
  def index
  end
end

The error:


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:

解决方案

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

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

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

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天全站免登陆