Ruby on rails Rspec错误:应至少有1个元素匹配“;表格[action=";/clients/1";][method=";post";][;,找到0
这里没有, 让我从我做的事情和原因开始: 客户和医务人员(他们是具有不同角色的用户)使用一个用户模型。我使用--skip迁移生成了客户机和医务人员,然后删除了自动生成的模型(client.rb和carparer.rb),并在生成的控制器和视图中将客户机替换为用户 一切似乎都很好,但我认为这只是表面现象。在测试视图时,我发现以下错误:Ruby on rails Rspec错误:应至少有1个元素匹配“;表格[action=";/clients/1";][method=";post";][;,找到0,ruby-on-rails,ruby,rspec,minitest,Ruby On Rails,Ruby,Rspec,Minitest,这里没有, 让我从我做的事情和原因开始: 客户和医务人员(他们是具有不同角色的用户)使用一个用户模型。我使用--skip迁移生成了客户机和医务人员,然后删除了自动生成的模型(client.rb和carparer.rb),并在生成的控制器和视图中将客户机替换为用户 一切似乎都很好,但我认为这只是表面现象。在测试视图时,我发现以下错误: Minitest::Assertion: Expected at least 1 element matching "form[action="/c
Minitest::Assertion:
Expected at least 1 element matching "form[action="/clients/1"][method="post"]", found 0..
Expected 0 to be >= 1.
以下是其他相关文件的内容:
控制器/客户端\控制器
class ClientsController < ApplicationController
# GET /clients/1/edit
def edit
end
end
views/clients/edit.html.haml
%h1 Editing client
= render 'form'
= link_to 'Show', @client
\|
= link_to 'Back', clients_path
= form_for @client do |f|
- if @client.errors.any?
#error_explanation
%h2= "#{pluralize(@client.errors.count, "error")} prohibited this client from being saved:"
%ul
- @client.errors.full_messages.each do |msg|
%li= msg
.actions
= f.submit 'Save'
视图/客户端/_form.html.haml
%h1 Editing client
= render 'form'
= link_to 'Show', @client
\|
= link_to 'Back', clients_path
= form_for @client do |f|
- if @client.errors.any?
#error_explanation
%h2= "#{pluralize(@client.errors.count, "error")} prohibited this client from being saved:"
%ul
- @client.errors.full_messages.each do |msg|
%li= msg
.actions
= f.submit 'Save'
删除现有模型时是否遗漏了任何内容?或者我该怎么做?一个模型和多个控制器。
如果需要其他文件
另外,我还没有接触过rails生成的视图。有趣的是,您正在使用rspec编写规范,虽然minitest中有错误,但以前从未进行过这种组合,但是您确定您正在运行正确的测试吗?使用rspec,您应该使用
bundle exec rspec
第二件事,您需要将这个由FactoryGirl创建的对象指定给视图中的实例变量
需要“rails\u助手”
describe 'clients/edit' do
let(:client) { FactoryGirl.create(:client_user) } # Prefer using let instead of before each, why? in case you make typo in `@client` you are going to get "nil" error, if you are going to make typo in `client` you are going to get Unknown Attribute/Method error. There are more advantages to it you can read about this in rspec documentation at https://www.relishapp.com/rspec/rspec-rails/docs
before do # before and before(:each) are basically the same
assign(:client, client) # now you need to assign instance variable in view so those are the same when you are doing the assertion below
render # render the view
end
it 'renders the edit client form' do
render
assert_select "form[action=?][method=?]", client_path(client), "post" do
end
# Rspec way
expect(rendered).to have_css('css selector goes here')
end
end
另外,顺便说一句,您不需要控制器中的空方法
edit
,rails会查找视图文件,因此如果您的文件夹中有edit.*文件,那么它将在没有这种难看的空方法的情况下工作。请提供详细信息。只需吐出整个存储库看起来就像是请求为您重写代码。@mudasobwa我应该在这里添加什么?搭建客户端并使用用户模型而不是客户端模型来复制它。其他是由rails generate scaffold客户端自动生成的。您应该删除上面的90%以提供MCVE。我想知道在scaffold和删除模型之后是否遗漏了一些内容,因为我认为这就是问题所在。对不起,看起来是这样的。我现在已经更新了。谢谢你的理解。现在请摆脱这个工厂,因为它工作得很好(因为client\u path(@client)
返回了/clients/1
),并分享正在测试的部分视图。嘿@mswiszcz,谢谢你的回复。我必须使用用户路径而不是客户端路径的任何方法assert_选择“form[action=?][method=?]”,用户路径(客户端),“post”do
I needed/users/1/而不是/clients/1。使用检查元素发现。