gpt4 book ai didi

ruby-on-rails - View 在浏览器中正确呈现,但在辅助方法上测试失败?

转载 作者:行者123 更新时间:2023-12-04 07:29:16 25 4
gpt4 key购买 nike

我有一个简单的 View 和一个定义标题的助手。如果我在浏览器中拉出 View ,一切正常,但 rspec 测试失败。这是我所拥有的:

这是我的测试:

describe PagesController do
render_views

before(:each) do
@base_title = "RoR Sample App"
end

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

it "should have the right title" do
get 'home'
response.should have_selector("title",
:content => @base_title + " | Home")
end
end
end

页面 Controller :
class PagesController < ApplicationController
def home
@title = "Home"
end

def contact
@title = "Contact"
end

def about
@title = "About"
end

def help
@title = "Help"
end
end

helper :
module ApplicationHelper
# Return a title on a per-page basis.
def title
base_title = "RoR22 Sample App"
if @title.nil?
base_title
else
"#{base_title} | #{@title}"
end
end
end

和观点:
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
</head>
<body>

<%= yield %>

</body>
</html>

这一切都在浏览器中正确呈现,但是当它到达 <%= title %> 时测试失败线。
 1) PagesController GET 'home' should be successful
Failure/Error: get 'home'
ActionView::Template::Error:
undefined local variable or method `title' for #<#<Class:0xabf0b1c>:0xabeec18>
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___418990135_90147100_123793781'
# ./spec/controllers/pages_controller_spec.rb:12:in `block (3 levels) in <top (required)>'

知道我做错了什么吗?

最佳答案

这是 spork 的一个已知问题。您可以在 prefork 块中使用此解决方法:

Spork.trap_method(Rails::Application, :reload_routes!)
Spork.trap_method(Rails::Application::RoutesReloader, :reload!)

关于ruby-on-rails - View 在浏览器中正确呈现,但在辅助方法上测试失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6628270/

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