gpt4 book ai didi

ruby-on-rails - 获取在 Rails 中进行测试的方法

转载 作者:行者123 更新时间:2023-11-28 20:18:31 24 4
gpt4 key购买 nike

我正在关注 RailsSpace: Building a Social Networking Website with Ruby on Rails by Michael Hartl .运行 rails v2.3.2.

我已经到了介绍测试的第 5 章。下面应该使用 get 方法将各个页面的标题与字符串匹配:

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

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

class SiteControllerTest < Test::Unit::TestCase
def setup
@controller = SiteController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end

def test_index
get :index
title = assigns(:title)
assert_equal "Welcome to RailsSpace!", title
assert_response :success
assert_template "index"
end

def test_about
get :title
title = assigns(:title)
assert_equal "About RailsSpace", title
assert_response :success
assert_template "about"
end

def test_help
get :help
title = assigns(:title)
assert_equal "RailsSpace Help", title
assert_response :success
assert_template "help"
end
end

在编译时我得到:

Loaded suite site_controller_test
Started
EEE
Finished in 0.057 seconds.

1) Error:
test_about(SiteControllerTest):
NoMethodError: undefined method `get' for #<SiteControllerTest:0x4854b30>
site_controller_test.rb:23:in `test_about'

2) Error:
test_help(SiteControllerTest):
NoMethodError: undefined method `get' for #<SiteControllerTest:0x4854b1c>
site_controller_test.rb:31:in `test_help'

3) Error:
test_index(SiteControllerTest):
NoMethodError: undefined method `get' for #<SiteControllerTest:0x485470c>
site_controller_test.rb:15:in `test_index'

3 tests, 0 assertions, 0 failures, 3 errors

其他人有过this issue唯一建议的解决方案就是重新安装。我对此不感兴趣。由于这是一本较旧的书,因此这可能只是 Rails 版本之间的破损。对于 rails v2.3.2,这相当于什么?

最佳答案

替换所有以下代码

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

class SiteControllerTest < Test::Unit::TestCase
def setup
@controller = SiteController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end

class SiteControllerTest < ActionController::TestCase

您使用的代码是指 Rails 2.0/2.1。

关于ruby-on-rails - 获取在 Rails 中进行测试的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1005150/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com