测试 RSpec 对 Model 的测试的最后一步

woody1983 · 2012年08月28日 · 最后由 woody1983 回复于 2012年08月28日 · 2568 次阅读

RSpec 对 Model 的测试的最后一步是不是以 save 对象为基准的?

describe "when name is too long" do
    before { @user.name = "a" * 51 }
    it { should_not be_valid }
  end

类似这样的测试 要验证是不是无效,RSpec 是不是会在 Test 库上做一个 save 的实际动作来验证?

不用啊,be_valid 调用的是 @user.valid? ,不需要保存

#1 楼 @ywencn 果然

irb(main):008:0> user.valid?
  User Exists (2.0ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
=> false
irb(main):009:0> user.invalid?
  User Exists (0.6ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
=> true

#2 楼 @woody1983 这个是有规律的,凡是叫 be_xxx 的,对应调用的就是 .xxx?

需要 登录 后方可回复, 如果你还没有账号请 注册新账号