开发者

How to Update an Attribute in Testing Model Using RSpec

I'm trapped on one spec in RSpec. How can I update a model's attribute to make it nil? Is better to validate under update controller spec?

Below is a sample code.

describe User do
  describe ".validation" do
    before(:each) do
      @user = User开发者_StackOverflow社区.create!({
        :username => "dexter_morgan"
      })
    end

    ...

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


This should be enough?

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜