使用rspec& amp;查看测试工厂女孩错误:没有路线匹配

时间:2013-09-16 19:15:15

标签: ruby-on-rails ruby-on-rails-3 rspec factory-bot rspec2

我对Ruby&轨;没有做太多的编程;但有技术背景。

我已经构建了一个非常少量的功能,主要是基于Railscasts,并希望使用TDD,并且我会回过头来尝试构建一些简单的测试,然后继续前进。

我坚持对视图进行测试,找不到类似的东西来弄清楚如何解决这个问题。我通过Railscasts,RSpec书等看过,并做了很多搜索。

所需的功能似乎工作正常,但测试失败。

感谢任何帮助或指示。

错误:

 Failure/Error: render
 ActionView::Template::Error:
   No route matches {:action=>"show", :controller=>"email_verifies", :id=>nil}
 # ./app/views/email_verifies/edit.html.haml:3:in `_app_views_email_verifies_edit_html_haml__3756516833416545914_70345184965780'
 # ./spec/views/email_verifies/edit.html.haml_spec.rb:6:in `block (2 levels) in <top (required)>'

代码:

应用程序/视图/ email_verifies / edit.html.haml

%h1 Verify Email Address

= form_for @user, :url => email_verify_path(params[:id]) do |f|

  .actions
    = f.submit "Verify Email Address"

规格/视图/ email_verifies / edit.html.haml_spec.rb

require 'spec_helper'

describe "email_verifies/edit.html.haml" do
  let(:user) { FactoryGirl.create(:user, :email_verify_token => "anything") }
  it "displays the text on the rendered page" do
    render
    rendered.should have_content("Verify Email Address")
  end
end

规格/工厂/ factories.rb

FactoryGirl.define do
  factory :user do
    sequence :email do |n|
      "foo#{n}@example.com" 
    end
    password "secret"
  end
end

app / controllers / email_verifies_controller.rb的一部分 (此控制器中没有'show'动作)

def edit
  @user = User.find_by_email_verify_token(params[:id])
  unless @user
    redirect_to signup_path, :alert => "This email verification has expired.  Please sign up again."
  end
end

部分路线:

edit_email_verify GET    /email_verifies/:id/edit(.:format)  email_verifies#edit  
     email_verify GET    /email_verifies/:id(.:format)       email_verifies#show  
email_verifies    GET    /email_verifies(.:format)           email_verifies#index  
edit_email_verify GET    /email_verifies/:id/edit(.:format)  email_verifies#edit  
     email_verify GET    /email_verifies/:id(.:format)       email_verifies#show  
                  PUT    /email_verifies/:id(.:format)       email_verifies#update

版本:

ruby​​ 1.9.3p362
铁轨(3.2.13)
factory_girl(4.2.0)
factory_girl_rails(4.2.1)
rspec(2.13.0)
rspec-core(2.13.1)
rspec-expectations(2.13.0)
rspec-mocks(2.13.1)
rspec-rails(2.13.2)

1 个答案:

答案 0 :(得分:0)

我对这种语法有点不熟悉,但根据错误,我认为问题在于:

  it "displays the text on the rendered page" do
    render #HERE!
    rendered.should have_content("Verify Email Address")
  end

在任何情况下,问题在于编辑操作(我认为,您在此处尝试测试的内容)需要:id,因此它知道要编辑的电子邮件 - 查看输出rake routes

edit_email_verify GET    /email_verifies/:id/edit(.:format) 

您需要在该呈现调用中包含:id - 这可能有效:

变化

it "displays the text on the rendered page" do
  render
  rendered.should have_content("Verify Email Address")
end

it "displays the text on the rendered page" do
  render edit_email_verify_path(@user) #the @user might need to be changed 
                                       #depending on what the `:id` refers to
  rendered.should have_content("Verify Email Address")
end

更新

  

我刚试过render edit_email_verify_path(:id => user.email_verify_token),似乎传递了正确的:id,然后收到了错误

ActionView::MissingTemplate: Missing partial /email_verifies/anything/edit with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :handlers=>[:erb, :builder, :coffee, :haml]} 

之前应该已经看过了,但问题在于:

Missing partial /email_verifies/****anything****/edit

:id中的/email_verifies/:id/edit是用户:id,因此您应该尝试

render edit_email_verify_path(user)

将返回/email_verifies/[your users id]/edit