【问题标题】:Devise/Rspec - Tested a user creation with (maybe) missing attributes (got true..)设计/Rspec - 测试了(可能)缺少属性的用户创建(得到了真实的..)
【发布时间】:2010-11-06 16:39:00
【问题描述】:

我正在使用 Micheal Hartl 源代码 (railstutorial) 使用 Rspec 测试设计

虽然启用了可确认模块,但我不明白为什么这个测试通过了:

spec/models/user_spec.rb

require 'spec_helper'

describe User do

  before(:each) do
    @attr = { :username => "ExampleUser", 
              :email => "user@example.com",
              :password => 'test1234',
            }
    end

  it "should create a new instance given valid attributes" do
    User.create!(@attr)
  end 
end

基本上,我想确定这段代码确实如此,它测试的是用户的创建,而不是这个验证(因为用户尚未确认并且测试返回 true)?是这样吗?

另外,我没有提供密码确认的属性,还是创建了用户!

这是否意味着 :validatable 模块中没有 (?):

validates :password, :confirmation => true

感谢您对此的看法!

【问题讨论】:

    标签: ruby-on-rails rspec devise


    【解决方案1】:

    一个问题是每个块末尾的尾随逗号。其次,您没有在测试中断言通过或失败测试的任何内容,尽管此时您可能会出错,这就是您说它没有通过的原因。

    您可以尝试将用户对象分配给变量:

      it "should create a new instance given valid attributes" do
        @user = User.new(@attr) 
        @user.should be_valid #=> new will let you know if its valid or not
        @user.save.should be_true #=> another possible assertion to pass/fail the test
        # debug message to give you back why it failed
        puts @user.errors.full_messages.to_sentence
      end 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多