【问题标题】:Writing a spec for helper with Ruby on Rails and RSpec使用 Ruby on Rails 和 RSpec 为助手编写规范
【发布时间】:2010-01-03 23:24:28
【问题描述】:

我一直在为控制器和模型编写规范,但我从未编写过辅助规范。我不知道从哪里开始。

我在application_helper.rb中有以下sn-p

  def title(page_title)
    content_for(:title) { page_title }
  end
  • 我应该如何为代码编写帮助规范?
  • 另外,如果有任何开源 Rails 应用程序可以显示良好的帮助测试/规范,请告诉我。

【问题讨论】:

    标签: ruby-on-rails ruby rspec bdd


    【解决方案1】:

    来自rspec-rails docs on Helper Specs

    Helper specs 存在于 spec/helpers 中,并混入其中 ActionView::TestCase::Behavior。

    提供一个辅助对象,它混合在辅助模块中 指定的,以及 ApplicationHelper(如果存在)。

    require 'spec_helper'
    describe ApplicationHelper do
      describe "#title" do
        it "displays the title" do
          # helper is an instance of ActionView::Base configured with the
          # ApplicationHelper and all of Rails' built-in helpers
          expect(helper.title).to match /Some Title/
        end
      end 
    end
    

    【讨论】:

      【解决方案2】:

      在指定助手时可以使用this syntax

      假设这是你的助手

      module ApplicationHelper
        def page_title
          @title || nil
        end
      end
      

      然后你可以用这个语法来指定它

      require "spec_helper"
      
      describe ApplicationHelper do
        describe "#page_title" do
          it "returns the instance variable" do
            assign(:title, "My Title")
            helper.page_title.should eql("My Title")
          end
        end
      end
      

      【讨论】:

        【解决方案3】:

        也可以在测试类中包含你的助手,如下所示:

         describe ApplicationHelper do
           helper ApplicationHelper
        
           it "should work" do
             my_helper_method("xyz").should == "result for xyz"
           end
         end
        

        使用 Rails 3 为我工作。

        【讨论】:

          【解决方案4】:

          当你“描述”它们时,RSpec 应该自动从你的 Rails 环境中加载类和模块,所以一个有效的帮助规范可能是:

          #deleted
          

          但请记住,bdd 不是在测试每一个方法,而是测试应用程序的行为。

          编辑:

          正如@Ken 所说,我的规范不正确,这绝对是错误的做法。所以我提出了一个我更喜欢的请求规范解决方案,而不是帮助规范。

          # inside your helper
          def title=(page_title)
            content_for(:title) { page_title }
          end
          
          # views/resource/index.html.erb
          <% title = "foo" %>
          
          # views/layouts/application.html.erb
          <%= yield :title %>
          
          # request spec
          require 'spec_helper'
          
          describe YourResource do
            it "should output content for title"
              get "/resource"
              response.body.should =~ /<title>foo<\/title>/
            end
          end
          

          否则,如果您只想测试辅助行为(因为它很关键或者因为您没有任何视图)@Ken 的解决方案会更好。

          【讨论】:

          • 非常感谢您的示例。正确的 BDD 并不是要涵盖所有方法。 Cucumber/Rcov 覆盖范围涉及大多数辅助方法。
          • 这并没有测试太多,因为 content_for 返回 nil。您应该呈现一个页面,并且 assert_select "title" 包含您期望的内容。
          • 这不是在测试帮助程序,这是在测试其他东西,根据您的示例,它看起来像是在测试视图文件(例如 index.html.erb)。助手测试应该只测试 ApplicationHelper#title 方法本身,而不依赖于视图或路由/控制器。
          • 我通常不测试简单的视图助手,我更喜欢请求规范,因为它们测试单个功能的行为。 @KenMayer,我已经更正了代码。
          【解决方案5】:

          用正则表达式解析 html 确实是在重新发明轮子。这对我来说工作量太大了:太不灵活,太容易出错。 (关于推理请看这个sarcastic but hilarious and accurate SO answer

          如果您需要解析助手输出的 html,您可以尝试 gem rspec-html-matchers。与 webrat 不同,它似乎与 RSpec 3 配合得很好。

          那么你可以:

          expect(helper.title).to have_tag('title', text: 'What you expect')
          

          【讨论】:

            猜你喜欢
            • 2015-03-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-07-13
            • 1970-01-01
            • 2014-03-20
            相关资源
            最近更新 更多