代码之家  ›  专栏  ›  技术社区  ›  benoitr

devieve/Rspec-测试了一个用户创建,其中(可能)缺少属性(得到true…)

  •  1
  • benoitr  · 技术社区  · 14 年前

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

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

    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)?这是对的?

    而且,我没有提供密码确认的属性,用户仍然是创建的!

    validates :password, :confirmation => true
    

    谢谢你看这个!

    1 回复  |  直到 14 年前
        1
  •  0
  •   Jed Schneider    14 年前

    一个问题是每个块末尾的尾随逗号。第二,你没有在测试中断言任何东西通过或不通过测试,尽管你可能在这一点上出错了,这就是为什么你说它没有通过。

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

      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