我在 lib 目录中创建了一个模块,我可以在我的 Rails 应用程序中自由调用它包含的各种方法(添加包含 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 => "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