首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby on Rails教程第8章注册成功

Ruby on Rails教程第8章注册成功
EN

Stack Overflow用户
提问于 2011-07-16 10:22:03
回答 2查看 821关注 0票数 0

我已经为用户控制器中的“成功创建”正确地编写了用户控制器和Spec测试,但是我得到了以下两个错误:

代码语言:javascript
复制
1) UsersController POST 'create' success should create a user
    Failure/Error: lambda do
      count should have been changed by 1, but was changed by 0
    # ./spec/controllers/users_controller_spec.rb:95:in `block (4 levels) in <top (required)>'

 2) UsersController POST 'create' success should redirect to the user show page
    Failure/Error: response.should redirect_to(user_path(assigns(:user)))
    ActionController::RoutingError:
      No route matches {:action=>"show", :controller=>"users", :id=>#<User id: nil, name: "New User", email: "user@example.com", created_at: nil, updated_at: nil, encrypted_password: nil, salt: nil>}
    # ./spec/controllers/users_controller_spec.rb:102:in `block (4 levels) in <top (required)>'

测试规格如下:

代码语言:javascript
复制
describe "success" do
   before (:each) do
       @attr = { :name => "New User" , 
                  :email => "user@example.com",
                  :password => "foobar",
                  :password_confirmation => "foobar"
                }
   end

   it  "should create a user" do
            lambda do
             post :create, :user => @attr
           end.should change(User, :count).by(1)
        end

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


   it "should have a welcome message" do
             post :create, :user => @attr
             flash[:success].should =~ /welcome to the sample app/i 
        end

   end 
end

控制器代码如下:

代码语言:javascript
复制
class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @title = @user.name
  end

  def new
    @user  = User.new
    @title = "Sign Up"
  end

  def create
     @user = User.new(params[:user])
     if @user.save
       redirect_to @user, :flash => { :success => "Welcome to the Sample App!" }
     else
       @title = "Sign up"
       render 'new'
     end
   end
end

我还得到了一个新的错误:

3) UsersController POST 'create‘success应该有欢迎消息

代码语言:javascript
复制
 Failure/Error: flash[:success].should =~ /welcome to the sample app/i
   expected: /welcome to the sample app/i
        got: nil (using =~)
 # ./spec/controllers/users_controller_spec.rb:107:in `block (4 levels) in <top (required)>'

下面是用户模型代码:

代码语言:javascript
复制
class User < ActiveRecord::Base

  attr_accessor :password

  attr_accessible :name, :email, :password, :password_confirmation

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :name, :presence => true, 
                   :length => {:maximum => 50}
  validates :email, :presence => true, 
                    :format => {:with => email_regex}, 
                    :uniqueness => {:case_sensitive => false}
  validates :password, :presence => true,
                       :confirmation => true,
                       :length => {:within => 6..40}

  before_save :encrypt_password

  def has_password?(submitted_password)
    encrypted_password == encrypt(submitted_password)
  end

  class << self
    def authenticate(email, submitted_password)
      user = User.find_by_email(email)
      return nil if user.nil?
      return user if user.has_password?(submitted_password)
    end
  end

出什么问题了?

EN

回答 2

Stack Overflow用户

发布于 2011-12-11 10:03:29

我也有同样的问题,对我来说问题就是电子邮件。我更改了它,测试又开始工作了。

票数 2
EN

Stack Overflow用户

发布于 2011-12-12 02:16:30

我正经历着完全相同的问题,它奇怪地消失了。我完全删除了“应该受到欢迎……”的部分。并再次复制(我建议您复制并粘贴以下代码):

代码语言:javascript
复制
  it "should have a welcome message" do
    post :create, :user => @attr
    flash[:success].should =~ /welcome to the sample app/i
  end

我知道它没有多大意义,但它解决了我的问题,现在一切都是绿色的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6714860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档