如何使用RSpec更新测试模型中的属性

时间:2011-07-05 12:38:52

标签: ruby-on-rails ruby testing rspec2 rspec-rails

我被困在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

1 个答案:

答案 0 :(得分: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