0

我只有 2 周的时间学习 ruby​​ 和 ruby​​ on rails。目前我的问题是与 te 路线匹配我不太了解,现在我正在使用 rspec 进行测试:

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=> "user@example.com"
    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
4

1 回答 1

2

在您的规范中,您两次提到用户(复数)而不是用户(单数):

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

应该通过。

更新

如果没有设置@attr,你当然应该正确设置它。只需为模型提供包含所有属性和值的哈希。假设用户模型具有属性名称和地址:

@attr = { :name => "SomeUserName", :address => "SomeAddress"}
于 2012-07-25T14:16:57.607 回答