ruby 中的 rspec 错误在每次执行之前

发布于 2024-12-13 08:36:20 字数 1635 浏览 1 评论 0原文

我正在阅读《通过示例学习 Rails》一书,并遇到了一个有趣的问题。如果我做错了什么,我会提前道歉。问题就在这里。

在第 3.5 节中,练习要求您执行以下操作:

您可能已经注意到 Pages 控制器规范中存在一些重复(清单 3.20)。特别是基本标题“Ruby on Rails 教程” 示例应用程序”对于每个标题测试都是相同的。使用 RSpec before(:each) 工具,在每个之前执行一段代码 测试用例,填写清单3.33来定义一个@base_title实例 消除这种重复的变量。 (此代码使用了两个新的 元素:符号、:each 和字符串连接运算符 +。 我们将在第 4 章中了解有关两者的更多信息,我们将在 before(:each) 中看到 再次在第 6.2.1 节中。)请注意,在捕获的基本标题中 实例变量,我们现在可以将 :content 与第一个对齐 每个左括号内的字符 (。这是我的首选 将代码格式化为多行的约定。

这是我的pages_controller_spec.rb 的样子:

describe PagesController do
  render_views

  before(:each) do
    # Define @base_title here.
    base_title = "Ruby on Rails Tutorial 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

当我加载rails 服务器并打开网页时,一切都很完美。标题按照基本标题应有的方式显示。但是,当我运行 rspec 时,我收到以下错误。我真的很想在 rspec 中解决这个问题。你觉得哪里不对?

  1) PagesController GET 'home' should have the right title
     Failure/Error: :content => @base_title + " | Home")
     NoMethodError:
       You have a nil object when you didn't expect it!
       You might have expected an instance of Array.
       The error occurred while evaluating nil.+
     # ./spec/controllers/pages_controller_spec.rb:20

Failed examples:

rspec ./spec/controllers/pages_controller_spec.rb:17 # PagesController GET 'home' should have the right title

I am working through the Learn Rails By Example book and came across an interesting problem. I'll apologize in advance if I am just doing something wrong. Here is the problem.

In section 3.5, the exercise it asks you to do the following:

You may have noticed some repetition in the Pages controller spec (Listing 3.20). In particular, the base title, “Ruby on Rails Tutorial
Sample App”, is the same for every title test. Using the RSpec
before(:each) facility, which executes a block of code before each
test case, fill in Listing 3.33 to define a @base_title instance
variable that eliminates this duplication. (This code uses two new
elements: a symbol, :each, and the string concatenation operator +.
We’ll learn more about both in Chapter 4, and we’ll see before(:each)
again in Section 6.2.1.) Note that, with the base title captured in an
instance variable, we are now able to align :content with the first
character inside each left parenthesis (. This is my preferred
convention for formatting code broken into multiple lines.

Here is how my pages_controller_spec.rb looks:

describe PagesController do
  render_views

  before(:each) do
    # Define @base_title here.
    base_title = "Ruby on Rails Tutorial 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

When I load the rails server and open the webpage, everything works perfect. The title shows up as it should per the base_title. However, when I run rspec, I receive the following errors. I'd really like to get this cleared up in rspec. What do you think is wrong?

  1) PagesController GET 'home' should have the right title
     Failure/Error: :content => @base_title + " | Home")
     NoMethodError:
       You have a nil object when you didn't expect it!
       You might have expected an instance of Array.
       The error occurred while evaluating nil.+
     # ./spec/controllers/pages_controller_spec.rb:20

Failed examples:

rspec ./spec/controllers/pages_controller_spec.rb:17 # PagesController GET 'home' should have the right title

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由范儿 2024-12-20 08:36:20

我认为您需要第 6 行的 @base_title = "Ruby on Rails Tutorial Sample App",而不是 base_title。您在那里定义的变量不会自动成为实例变量

I think you need @base_title = "Ruby on Rails Tutorial Sample App" at line 6, not base_title. A variable defined as you do there does not automatically become an instance variable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文