0

私は Ruby と Rails にかなり慣れていません。あまりプログラミングをしたことがありません。しかし、技術的なバックグラウンドを持っています。

私は主に 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)>'

コード:

app/views/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"

spec/views/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 の一部 (このコントローラーには「表示」アクションはありません)

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
rails (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 レール (2.13.2)

4

1 に答える 1

1

私はこの構文に少し慣れていませんが、エラーに基づいて、問題はここにあると思います:

  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その render 呼び出しにを含める必要があります-これはうまくいくかもしれません:

変化する

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

:idin/email_verifies/:id/editはユーザーなので、:id試してみてください

render edit_email_verify_path(user)

戻ります/email_verifies/[your users id]/edit

于 2013-09-16T19:22:58.627 に答える