3

我被困在 RSpec 中的一个规范上。如何更新模型的属性以使其为零?在更新控制器规范下验证是否更好?

下面是一个示例代码。

describe User do
  describe ".validation" do
    before(:each) do
      @user = User.create!({
        :username => "dexter_morgan"
      })
    end

    ...

    context "given invalid attributes" do
      # how can I make the username nil?
      it "rejects blank username"
    end
  end
end
4

1 回答 1

5

这应该够了吧?

describe User do
  describe ".validation" do
    before(:each) do
      @user = User.create!({
        :username => "dexter_morgan"
      })
    end

    ...

    context "given invalid attributes" do
      it "rejects blank username" do
        @user.username = nil
        @user.should_not be_valid
      end
    end
  end
end
于 2011-07-05T12:58:46.973 回答