【发布时间】:2018-03-06 13:20:08
【问题描述】:
我习惯于编写视图规范,这些规范至少会检查以下内容:
expect(view).to receive(:title).with('Required page title here')
(title 是我为设置页面标题而编写的辅助方法。)现在我正在尝试为我的 Devise 视图编写规范,如下所示:
- title 'Lost Password'
.row
.col-lg-6
= form_for resource, as: resource_name, url: password_path(resource_name) do |f|
= render 'layouts/errors', object: resource
.form-group
= f.label :email
= f.text_field :email, class: 'form-control', autofocus: true
= f.submit 'Send me the instructions', class: 'btn btn-primary'
%hr
= render 'devise/shared/links'
.col-lg-6
= render 'devise/shared/advantages'
resource 和 resource_name 由 Devise 定义。
如果我在视图上运行以下规范:
require 'rails_helper'
describe 'devise/passwords/new', type: :view do
it 'sets the page title' do
expect(view).to receive(:title).with('Lost Password')
render
end
end
上面写着:undefined local variable or method 'resource'。我试过了:
allow(view).to receive(:resource).and_return(User.new)
然后我得到[snip, long class definition] does not implement: resource。
我该如何进行这项工作?我不想将 Capybara 用于这种微不足道的事情。
【问题讨论】:
-
您是否以标准的开箱即用方式实现您的设计视图?或者您正在使用自定义视图? stackoverflow.com/questions/4081744/…
-
@JamesMilani 我正在使用生成器输出的视图,针对 Bootstrap 稍作修改。我已经用相关的视图代码更新了问题。
-
嗨@janosrusiczki,你能发布整个测试吗?
allow(view).to receive(:resource).and_return(User.new)看起来不错。 -
我没有用
title得到它。我在您的视图示例中找不到它 -
我认为这个答案可能会对您有所帮助:stackoverflow.com/questions/4081744/…
标签: ruby-on-rails rspec devise