Rails-为什么不能在测试中使用在模块中创建的方法? [英] Rails - Why can't I use a method I created in a module in my tests?

查看:66
本文介绍了Rails-为什么不能在测试中使用在模块中创建的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在lib目录中创建了一个模块,我可以自由地在整个Rails应用程序中调用它包含的各种方法(添加include ModuleName之后).

I created a module in lib directory and I can freely call the various methods it contains throughout my Rails app (after adding include ModuleName) with no problems.

但是,对于测试,他们抱怨不存在这样的方法.我很幸运地尝试将模块包含到我的测试助手中.谁能帮忙.

However when it comes to tests, they complain no such methods exist. I tried including the module into my test helper with no luck. Can anyone help.

4) Error:
test_valid_signup_redirects_user_to_spreedly(UsersControllerTest):
NoMethodError: undefined method `spreedly_signup_url' for SpreedlyTools:Module
    /test/functional/user_controller_test.rb:119:in `test_valid_signup_redirects_user_to_spreedly'


module SpreedlyTools
  protected
  def spreedly_signup_url(user)
    return "blahblah"
  end
end


class ApplicationController < ActionController::Base


  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details
  include SpreedlyTools
  ....
end


ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'test_help'

class ActiveSupport::TestCase
   include SpreedlyTools
   ....
end


require File.dirname(__FILE__) + '/../test_helper'
require 'users_controller'

# Re-raise errors caught by the controller.
class UsersController; def rescue_action(e) raise e end; end

class UsersControllerTest < ActionController::TestCase
....

  def test_valid_signup_redirects_user_to_spreedly
    get :new
    assert_response :success
    post :create, :user =>  {:first_name => "bobby",
                      :last_name => "brown",
                      :email => "bobby.brown@gmail.com",
                      :email_confirmation => "bobby.brown@gmail.com",
                      :password => "bobby1",
                      :password_confirmation => "bobby1"}

    assert_response :redirect
    user = assigns(:user)
    assert_redirected_to SpreedlyTools.spreedly_signup_url(user)

  end
end

推荐答案

这里有几个不同的错误. 首先,当您创建模块并将模块的内容混合到一个类中时,模块中的方法将成为该类本身的一部分.

There are a couple of different errors here. First, when you create a module and you mix the content of the module into a class, the methods within the module become part of the class itself.

也就是说,以下行没有意义

That said, the following line doesn't make sense

assert_redirected_to SpreedlyTools.spreedly_signup_url(user)

假设您将模块混合到User类中,则应将方法调用为

Assuming you mixed the module into a User class, you should call the method as

assert_redirected_to User.new.spreedly_signup_url(user)

也请注意new语句.因为您将模块include放入类中,而没有extend进入类,所以该方法成为实例方法而不是类方法.

Also note the new statement. Because you include the module into the class and you don't extend the class, the method become an instance method not a class method.

assert_redirected_to User.new.spreedly_signup_url(user) # valid
assert_redirected_to User.spreedly_signup_url(user) # invalid

因此,以下行没有意义.

For this reason, the following line doesn't make sense.

class ActiveSupport::TestCase
   include SpreedlyTools
   ....
end

这篇关于Rails-为什么不能在测试中使用在模块中创建的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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