2012-07-25 61 views
0

我只有2周的時間學習紅寶石和紅寶石。 目前我的問題是與TE路由匹配我不明白這麼多,現在我使用RSpec這個測試:測試:沒有路線匹配{:action =>「show」,:controller =>「users」,:id => nil}

require 'spec_helper' 

describe UsersController do 

it "should redirect to the user show page" do 
post :create, :users => @attr 
response.should redirect_to(user_path(assigns(:users))) 
end 
    describe "signup" do 

before { visit new_user_registration_path } 

let(:submit) { "Sign up" } 

describe "with invalid information" do 
    it "should not create a user" do 
    expect { click_button submit }.not_to change(User, :count) 
    end 
end 

describe "with valid information" do 
    before do 
    fill_in "Email", :with=> "[email protected]" 
    fill_in "Password", :with=> "foobar" 
    #fill_in "password_confirmation", :with=> "foobar" 
    end 

    it "should create a user" do 
    expect { click_button submit }.to change(User, :count).by(1) 
    end 
end 
end 
end 

我有這個在我的UserController的

class UsersController < ApplicationController 

def new 
    @user = User.new 
end 

def create 
    @user = User.new(params[:user]) 
    if @user.save 
     redirect_to user_session_path 
    else 
    redirect_to new_user_session_path 
end 

end 

def show 
    @user = User.find(params[:id]) 
    #redirect_to @user 
end 
end 

但是當我運行測試我得到這個錯誤:

Failures: 

1) UsersController should redirect to the user show page 
    Failure/Error: response.should redirect_to(user_path(assigns(:users))) 
    ActionController::RoutingError: 
    No route matches {:action=>"show", :controller=>"users", :id=>nil} 
    # ./spec/controllers/user_controller_spec.rb:7 

2) UsersController signup with valid information should create a user 
    Failure/Error: expect { click_button submit }.to change(User, :count).by(1) 
    count should have been changed by 1, but was changed by 0 
    # ./spec/controllers/user_controller_spec.rb:29 

回答

2

在你的天賦你指兩次給用戶(複數),而不是用戶(單數):

it "should redirect to the user show page" do 
    post :create, :user => @attr 
    response.should redirect_to(user_path(assigns(:user))) 
end 

應該通過。

UPDATE

如果@attr未設置你當然應該設置正確。只需提供一個包含模型所有屬性和值的散列。假設用戶模型具有屬性的名稱和地址:

@attr = { :name => "SomeUserName", :address => "SomeAddress"} 
+0

我改變了它,現在顯示了同樣的錯誤,但就像這樣: 沒有路由匹配{:控制器=>「用戶」,:ID =>#<用戶ID:無,電子郵件:「」,encrypted_pa​​ssword:「」,reset_password_token:無,reset_password_sent_at:無,remember_created_at:無,sign_in_count:0,current_sign_in_at:nil,last_sign_in_at:nil,current_sign_in_ip:無,last_sign_in_ip:無,名稱:無,created_at:nil,updated_at:nil>,:action =>「show」} – Asantoya17 2012-07-25 14:21:10

+0

您是否也更改爲: 'post:create,:user => @ attr'? 另外你在哪裏設置@attr? – 2012-07-25 14:23:13

+0

是的,我做了你告訴我 – Asantoya17 2012-07-25 14:24:41

相關問題