Rails - 为什么我不能在测试中使用在模块中创建的方法?
我在 lib 目录中创建了一个模块,我可以在整个 Rails 应用程序中自由调用它包含的各种方法(添加 include ModuleName 后),没有任何问题。
然而,当谈到测试时,他们抱怨不存在这样的方法。我尝试将该模块包含到我的测试助手中,但没有成功。任何人都可以帮忙。
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 => "[email protected]",
:email_confirmation => "[email protected]",
:password => "bobby1",
:password_confirmation => "bobby1"}
assert_response :redirect
user = assigns(:user)
assert_redirected_to SpreedlyTools.spreedly_signup_url(user)
end
end
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 => "[email protected]",
:email_confirmation => "[email protected]",
:password => "bobby1",
:password_confirmation => "bobby1"}
assert_response :redirect
user = assigns(:user)
assert_redirected_to SpreedlyTools.spreedly_signup_url(user)
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有几个不同的错误。
首先,当您创建模块并将模块的内容混合到类中时,模块中的方法将成为类本身的一部分。
也就是说,下面的行没有意义
假设您将模块混合到 User 类中,您应该调用该方法,
另请注意
new
语句。因为您将模块包含
到类中并且没有扩展
该类,所以该方法成为实例方法而不是类方法。因此,下面的行没有意义。
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
Assuming you mixed the module into a User class, you should call the method as
Also note the
new
statement. Because youinclude
the module into the class and you don'textend
the class, the method become an instance method not a class method.For this reason, the following line doesn't make sense.
啊,这是一个微妙的问题,但很容易纠正。您想要测试由包含 SpreedlyTools 的类创建的对象,而不是在测试套件中包含 SpreedlyTools。
在 Ruby 中,您可能会发现类的工作方式比您想象的模块的工作方式要多得多。您可以通过如下定义来使用类级别函数:
这现在有意义了。
模块用于将代码混合到其他类中。但是,如果您只想随时使用通用工具,那么您确实需要类级别的功能。
Ah, it's a subtle problem but easily corrected. Rather than including SpreedlyTools in your test suite you want to test objects that were created by classes that include SpreedlyTools.
In Ruby you may find that Classes work a lot more the way you're thinking Modules work. You can use a class level function by defining it as such:
This would make sense now.
Modules are for mixing in code to other classes. But if you just want generic tools available at all times you really want class level functions.