- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我对 Controller 规范中的每个 HTTP 方法/ Controller 操作组合重复了一次以下测试:
it "requires authentication" do
get :show, id: project.id
# Unauthenticated users should be redirected to the login page
expect(response).to redirect_to new_user_session_path
end
我找到了以下三种方法来重构它并消除重复。哪个最合适?
在我看来,共享示例是最合适的解决方案。但是,必须使用 block 才能将 params
传递给共享示例,感觉有点尴尬。
shared_examples "requires authentication" do |http_method, action|
it "requires authentication" do
process(action, http_method.to_s, params)
expect(response).to redirect_to new_user_session_path
end
end
RSpec.describe ProjectsController, type: :controller do
describe "GET show", :focus do
let(:project) { Project.create(name: "Project Rigpa") }
include_examples "requires authentication", :GET, :show do
let(:params) { {id: project.id} }
end
end
end
这样做的好处是不需要 block 将 project.id
传递给辅助方法。
RSpec.describe ProjectsController, type: :controller do
def require_authentication(http_method, action, params)
process(action, http_method.to_s, params)
expect(response).to redirect_to new_user_session_path
end
describe "GET show", :focus do
let(:project) { Project.create(name: "Project Rigpa") }
it "requires authentication" do
require_authentication(:GET, :show, id: project.id )
end
end
end
要是有单行测试就好了。
RSpec::Matchers.define :require_authentication do |http_method, action, params|
match do
process(action, http_method.to_s, params)
expect(response).to redirect_to Rails.application.routes.url_helpers.new_user_session_path
end
end
RSpec.describe ProjectsController, type: :controller do
describe "GET show", :focus do
let(:project) { Project.create(name: "Project Rigpa") }
it { is_expected.to require_authentication(:GET, :show, {id: project.id}) }
end
end
提前致谢。
最佳答案
didroe 在 this Reddit post 中提供的建议让我想到将方法/操作调用 (process
) 放在共享代码中并不是一个好主意,因为它增加了复杂性(降低了可读性)并且实际上并没有减少代码重复。
再搜索一些之后,我在 Everyday Rails Testing with RSpec by Aaron Sumner 中找到了我认为最好的选择。书(第 102 页)。
创建以下自定义匹配器:
# spec/support/matchers/require_login.rb
RSpec::Matchers.define :require_login do |expected|
match do |actual|
expect(actual).to redirect_to \
Rails.application.routes.url_helpers.new_user_session_path
end
failure_message do |actual|
"expected to require login to access the method"
end
failure_message_when_negated do |actual|
"expected not to require login to access the method"
end
description do
"redirect to the login form"
end
end
并对每个 Controller 的每个操作使用如下测试:
it "requires authentication" do
get :show, id: project.id
expect(response).to require_login
end
与在所有测试中重复expect(response).to redirect_to new_user_session_path
相比,这种方式有以下优点:
你怎么看?
关于ruby-on-rails - Rails RSpec,DRY 规范 : shared example vs. 辅助方法与自定义匹配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44597682/
我正在对 Share 开发和执行一些自定义。我的 IDE 是 Eclipse Juno,工作区由以下元素组成: 露天网络项目 扩展 Java 项目 分享网站项目 alfresco 和 share we
这是我一直面临的一个非常奇怪的问题。我正在使用 Share.share,它将消息、主题 (ios) 和标题(对于 gmail)作为参数。在android中它工作正常。即主题行是“我是标题”,而消息正文
当在static上下文中未使用而不是时(也就是说,当static关键字不存在并且您不在全局范围内时),shared和__gshared关键字有什么作用? 例子: struct Temp {
我想知道如何从 Surf 中的 Root 对象知道当前主机名。 我正在编写一个服务于 JNLP 的网络脚本,所以我没有页面上下文,并且 ${url.context} 只返回 /share。 我正在寻找
我想知道如何从 Surf 中的 Root 对象知道当前主机名。 我正在编写一个服务于 JNLP 的网络脚本,所以我没有页面上下文,并且 ${url.context} 只返回 /share。 我正在寻找
我需要在 Alfresco Share 上下文中创建一个无需身份验证即可访问的页面。使用页面框架时,它看起来非常简单,因为您可以添加 none到页面定义。 当使用 aikau 时,页面定义消失了,我只
我有执行REST命令并处理结果的python脚本。我希望该脚本可被不同的Jenkins Pipelines使用,我通过Jenkins官方文档发现的一种方式是使用“共享库”,而这些示例(以及我在网上找到
查看此函数(矩阵 vector 积): std::vector times(std::vector > const& A, std::vector const& b, int m, int n) {
我正在尝试弄清楚如何与 Alfresco Share 执行一些 dashlet 间通信。 这是一个简单的用例: 我们确实有 2 个 dashlet,我们称它们为 A 和 B。我希望能够在 A 中填写字
是否可以在 Snowflake 的 FUNCTION 或 PROCEDURE 中发出“Show Shares”函数调用?由于信息架构中没有元数据对象来查询有关共享的信息,因此我们正在寻找一种方法来创建
我对“无共享”分布式系统的理解是集群中的每个节点都有自己的磁盘可以持久化。如果是这种情况,我不确定为什么拥有这样的架构会有好处,因为您不仅需要在应用程序级别进行复制/负载平衡/集群,而且还需要在磁盘上
我对“无共享”分布式系统的理解是集群中的每个节点都有自己的磁盘可以持久化。如果是这种情况,我不确定为什么拥有这样的架构会有好处,因为您不仅需要在应用程序级别进行复制/负载平衡/集群,而且还需要在磁盘上
在 Alfresco 4.0 中,我想扩展 Share Doclib Filter webscript 以添加我自己的自定义过滤器。 有没有一种简单的方法可以在 share-config-custom
假设我要匹配: PREFIXsomething 或: somethingPOSTFIX 但肯定不是: PREFIXsomethingPOSTFIX 其中 something 是某种共享模式,而 PRE
我正在寻找一种使用 JavaScript 在 google drive sdk 上查找用户共享文件列表的方法。 重要提示:我不是在寻找“与我共享”的文件列表。 我目前所拥有的列出了“与我共享”的文件,
最近刚开始使用 swifting,在使用应用程序组在 iOS 设备之间共享数据时遇到了问题。 基本上我已经按照以下步骤设置了项目: [iPhone] 为 iPhone 目标启用应用程序组 初始化数据如
我知道 -Wl,-shared 是 ld 的一个选项。我见过有人这样编译, $ gcc -shared -Wl,-soname,libtest.so -o libtest.so *.o 还有这样的人
我想知道对这句名言最接地气的解释是什么: Don't communicate by sharing memory; share memory by communicating. (R. Pike) 在
我不确定我在这里做错了什么,我认为应该有更多的文档或关于这个Web Share API的更好的错误描述。。我正在尝试共享以下文件。使用。我已经确保该类型是允许的类型,但我一直收到DOMExceptio
我正在研究 RxSwift 和 RxCocoa。官方文档中有这些解释。 分享副作用 分享资源 它们是一样的吗?如果有区别,那有什么区别?光看官方文档的解释,我看不懂。 预先感谢您的回答。 最佳答案 文
我是一名优秀的程序员,十分优秀!