ดูการทดสอบโดยใช้ข้อผิดพลาด rspec และโรงงานสาว: ไม่มีเส้นทางที่ตรงกัน

ฉันค่อนข้างใหม่กับ Ruby & Rails; ยังไม่ได้เขียนโปรแกรมมากนัก แต่มีพื้นฐานด้านเทคโนโลยี

ฉันได้สร้างฟังก์ชันการทำงานเพียงเล็กน้อยซึ่งส่วนใหญ่ใช้ Railscasts และต้องการใช้ TDD และกำลังย้อนกลับไปและพยายามสร้างการทดสอบง่ายๆ ก่อนที่จะก้าวไปข้างหน้า

ฉันติดอยู่กับการทดสอบมุมมองและไม่พบสิ่งที่คล้ายกันมากพอที่จะทราบวิธีแก้ไขปัญหานี้ ฉันได้ดู Railscasts หนังสือ The 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_verify/edit.html.haml

%h1 Verify Email Address

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

  .actions
    = f.submit "Verify Email Address"

ข้อมูลจำเพาะ/views/email_verify/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

ข้อมูลจำเพาะ/โรงงาน/factory.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

เวอร์ชัน:

ทับทิม 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)


person jbbythebch    schedule 16.09.2013    source แหล่งที่มา


คำตอบ (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 ในการเรนเดอร์การโทรนั้น - สิ่งนี้อาจใช้ได้:

เปลี่ยน

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

to

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

person dax    schedule 16.09.2013
comment
ขอบคุณ. ฉันคิดว่าคุณพูดถูก แต่ฉันได้ลองค้นหารายละเอียดเกี่ยวกับไวยากรณ์การเรนเดอร์แล้ว แต่ยังไม่พบสิ่งที่ใช้ได้ผล ฉันไม่พบตัวอย่างที่คล้ายกันเช่นกัน ดูเหมือนว่ามันควรจะง่าย แต่ฉันมีปัญหากับมัน - person jbbythebch; 16.09.2013
comment
ความคืบหน้าเล็กน้อย...ข้อผิดพลาดเปลี่ยนเป็น: No route matches {:action=>"edit", :controller=>"email_verifies", :id=>nil} เท่าที่ฉันสามารถบอกได้ :id ควรเป็น email_verify_token แต่จะไม่ถูกส่งผ่านโดยใช้การเรนเดอร์ เมื่อฉันทดสอบโค้ดที่คล้ายกันซึ่งสร้างตัวแปรแทนที่จะใช้ 'let' มันเขียน email_verify_token อย่างถูกต้องไปยัง db ทดสอบ - person jbbythebch; 17.09.2013
comment
ฉันเพิ่งลอง 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]} - person jbbythebch; 17.09.2013
comment
ฉันคิดว่ามันอยู่ในตำแหน่งที่ถูกต้อง เป็นรายการแรกที่แสดงไว้ด้านบนภายใต้ 'รหัส:' และฉันก็ใส่เส้นทางไว้เหนือโค้ดในกรณีที่เป็นปัญหา ซึ่งก็คือ: app/views/email_verifies/edit.html.haml - person jbbythebch; 17.09.2013
comment
การอัปเดตของคุณอาจเป็นสมมติฐานที่ดี แต่ในกรณีนี้ จริงๆ แล้วใช้ 'email_verify_token' เป็น ':id' ที่ส่งผ่านไปยังการดำเนินการแก้ไขในคอนโทรลเลอร์เพื่อทำการค้นหาด้วยโทเค็นนั้นตามที่เห็นในบรรทัดนี้ (แสดงด้านบนใน ส่วนรหัส): @user = User.find_by_email_verify_token(params[:id]); อย่างน้อยนั่นคือความเข้าใจของฉันในเรื่องนี้ และในเบราว์เซอร์ที่ทำงานบนเซิร์ฟเวอร์ dev ในเครื่อง โทเค็นนั้นอยู่ใน url - person jbbythebch; 18.09.2013
comment
มันแสดงให้เห็นว่าในเบราว์เซอร์ แต่มันใช้งานได้เพราะ Rails ฉลาดพอที่จะดึงสิ่งที่ต้องการ - ข้อผิดพลาดของคุณคือ Missing partial /email_verifies/anything/edit - ลองนึกถึงสิ่งที่พูด - มันไม่พบวัตถุที่จะแก้ไข และวิธีที่มันจะค้นหามัน จะเป็นผ่าน :id - ในกรณีนี้คือรหัสผู้ใช้ ลองดูว่ามันใช้งานได้หรือไม่ (อย่างน้อยก็ในชุดทดสอบ) - person dax; 18.09.2013
comment
ขออภัยสำหรับการตอบกลับล่าช้า @dax ฉันลอง render edit_email_verify_path(:id => user.id) แล้วพบข้อผิดพลาดเดียวกันนี้ค่อนข้างมาก: Missing partial /email_verifies/3/edit with {:locale=>[:en], :formats=>[:html, ... - person jbbythebch; 18.09.2013
comment
ลองดูคำถามนี้: stackoverflow.com/questions /10943744/ - person dax; 18.09.2013
comment
ฉันพยายามคัดลอก app/views/email_verifies/edit.html.haml ไปยัง (เส้นทางเดียวกัน) _edit.html.haml และแม้แต่ _edit.html, & _edit.haml ในกรณีนี้ และได้รับข้อผิดพลาดเดียวกันกับด้านบน Missing partial /email_verifies/anything/edit with {:locale=>[:en], :formats=>[:html, ... ฉันพบคำถาม (ในความคิดเห็นถัดไป พื้นที่ไม่เพียงพอ) ซึ่ง 'nathanvda' พูดว่า 'ประการที่สอง เว้นแต่ว่ามุมมองของคุณจะเป็นบางส่วนที่คาดหวังตัวแปร คุณควรเขียน' render (ละรหัสต่อไปนี้ออกไปทั้งหมด) ดังนั้น ฉันสงสัยว่าในโค้ดของเรา render edit_email_verify_path... เหตุใดจึงมองหาบางส่วน - person jbbythebch; 19.09.2013
comment
คำถามที่เกี่ยวข้อง: stackoverflow.com/questions/8187944 / แม้ว่าฉันจะซาบซึ้งในความพากเพียรของคุณ @dax และเกลียดการยอมแพ้ ฉันเริ่มคิดว่าเราอาจต้องการหยุดถามคำถามนี้ และบางทีหลังจากที่ฉันมีประสบการณ์มากขึ้น ฉันจึงจะสามารถเขียนใหม่ได้ การทดสอบหรือตามที่ผู้โพสต์คนสุดท้ายในคำถามอ้างอิงด้านบนบอกว่า เพียงแค่หยุดเขียนข้อกำหนดการดูทั้งหมดแล้วใช้ Cucumber เนื่องจาก 'สิ่งเหล่านี้เขียนยากโดยไม่จำเป็น และพวกเขาไม่ได้ทดสอบเพียงพอที่จะคุ้มค่าเลย' - person jbbythebch; 19.09.2013