gpt4 book ai didi

ruby - 不断收到 @controller is nil 错误

转载 作者:数据小太阳 更新时间:2023-10-29 07:53:28 25 4
gpt4 key购买 nike

在尝试运行 Rspec 测试作为 Rubyonrails 教程的一部分时,我不断收到此错误

失败:

1) GET 'about' 应该成功

 Failure/Error: get 'about'
RuntimeError:
@controller is nil: make sure you set it in your test's setup method.
# ./spec/controllers/pages_controller_spec.rb:27:in `block (2 levels) in <top (required)>'

我已经尽我所能尝试纠正它,但都证明是徒劳的。任何帮助将不胜感激。

这是我的 spec_helper.rb 文件

require 'rubygems'
require 'spork'

Spork.prefork do
end
Spork.each_run do
end

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
config.include RSpec::Rails::ControllerExampleGroup
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
end

pages_controller.rb 文件:

class PagesController < ApplicationController
def home
end

def contact
end

def about
end
end

pages_controller_spec.rb 文件:

require 'spec_helper'

describe PagesController do
render_views

describe "GET 'home'" do
render_views
it "should be successful" do
get 'home'
response.should be_success
end
end

describe "GET 'contact'" do
render_views
it "should be successful" do
get 'contact'
response.should be_success
end
end

end

describe "GET 'about'" do
render_views
it "should be successful" do
get 'about'
response.should be_success
end
end

routes.rb 文件:

SampleApp::Application.routes.draw do
get "pages/home"

get "pages/contact"

get "pages/about"

end

顺便说一句,我运行 Spork 和自动测试没有问题。

最佳答案

问题在于测试中的嵌套:describe PagesController block 仅包含前两个测试而不包含最后一个。

它应该是这样的:

describe PagesController do
render_views

describe "GET 'home'" do
it "should be successful" do
get 'home'
response.should be_success
end
end

describe "GET 'contact'" do
it "should be successful" do
get 'contact'
response.should be_success
end
end

describe "GET 'about'" do
it "should be successful" do
get 'about'
response.should be_success
end
end
end

参见:How do i get rid of @controller is nil error in my tests

附注我已经取出所有 render_views 除了顶级 describe block 中的那个 - 你应该只需要为 block 调用一次(参见 specs ) .

关于ruby - 不断收到 @controller is nil 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14267506/

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