- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在构建 Rails 应用程序并使用 RSpec 制定测试。
我为我正在创建的名为 current_link_to
的方法编写了测试。此方法应该检查当前页面是否对应于我传递给它的路径,并将 current
类添加到生成的链接中,以防它匹配。
这是规范:
require "spec_helper"
describe ApplicationHelper do
describe "#current_link_to" do
let(:name) { "Products" }
let(:path) { products_path }
let(:rendered) { current_link_to(name, path) }
context "when the given path is the current path" do
before { visit(path) }
it "should return a link with the current class" do
# Uses the gem "rspec-html-matchers" (https://github.com/kucaahbe/rspec-html-matchers)
expect(rendered).to have_tag("a", with: { href: path, class: "current" }) do
with_text(name)
end
end
end
context "when the given path is not the current path" do
before { visit(about_path) }
it "should return a link without the current class" do
expect(rendered).to have_tag("a", with: { href: path }, without: { class: "current" } ) do
with_text(name)
end
end
end
end
end
然后我尝试按照规范实现我的方法:
module ApplicationHelper
def current_link_to(name, path, options={})
options.merge!({ class: "#{options[:class]} current".strip }) if current_page?(path)
link_to(name, path, options)
end
end
但是,测试失败并出现以下错误:
Failure/Error: let(:rendered) { current_link_to(name, path) }
RuntimeError: You cannot use helpers that need to determine the current page unless your view context provides a Request object in a #request method
因为我真的不需要 current_page?
辅助方法来对请求执行检查,所以我决定将它 stub 是有意义的。
我尝试了以下方法,但均无效:
helper.double(:current_page? => true)
helper.current_page?
方法,但它与我的函数调用的方法不同。allow(ActionView::Helpers::UrlHelper).to receive(:current_page?).and_return(true)
在写这个问题时我偶然发现了解决方案。我设法在 before
block 中使用它来 stub current_page?
方法:
allow(self).to receive(:current_page?).and_return(true)
它奏效了,但是这个解决方案提出的问题比它真正回答的要多。我现在对它是如何工作的感到困惑,因为 self
在 中看起来很奇怪>before
block 将响应 current_page?
并且该方法实际上与我的助手正在调用的方法完全相同。
即使阅读了文档并试图通过使用 puts
调用来弄清楚它是如何工作的,以下疑虑仍然困扰着我:
helper
对象的方法提供?before
block 中对 self
设置 current_page?
方法如何以某种方式反射(reflect)到我的助手调用的实际方法上?我的助手中的 self
是否出于某种原因引用了您可以在 before
block 中找到的相同 self
? RSpec 或 Rails 是否在幕后包含和混合了东西?self
包含我的规范和我的助手,那么在这种情况下 self
到底指的是什么?为什么到处都一样?如果有人能帮我解决这个问题就太好了,因为这让我大吃一惊,而且我害怕使用我并不真正理解的代码。
最佳答案
恕我直言,您在这里测试的功能有点太多了。诀窍是只测试您需要测试的位。
在这种情况下,您只需要测试当前类是否在需要时添加,而不是在不需要时添加。
这段代码应该可以为您解决问题:
require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the ApplicationHelper.
RSpec.describe ApplicationHelper, type: :helper do
describe 'current_link_to' do
let(:subject) { helper.current_link_to('some_name', 'some_path', options = {}) }
context 'where the path is current' do
before do
allow(helper).to receive(:current_page?).and_return true
end
it 'should include the current class' do
expect(subject).to match /current/
end
end
context 'where the path is not current' do
before do
allow(helper).to receive(:current_page?).and_return false
end
it 'should not include the current class' do
expect(subject).to_not match /current/
end
end
end
end
我有点油嘴滑舌,只测试了返回字符串中是否存在“current”。如果您想要更精确,可以测试类似“class="current"' 的内容。
另一个关键是页面顶部的注释,Rails 会为您将其插入到空白的帮助规范中:
# Specs in this file have access to a helper object that includes
# the ApplicationHelper.
这意味着您可以在上面的评论中使用“self”的地方使用“helper”,这让事情变得更清晰(恕我直言)
希望对您有所帮助!
关于ruby-on-rails - 从帮助器规范中 stub 一个帮助器方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21956762/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!