开发者

How to Update an Attribute in Testing Model Using RSpec

开发者 https://www.devze.com 2023-03-17 04:16 出处:网络
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?

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

精彩评论

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