Ben
Ben

Reputation: 1368

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.create!({
        :username => "dexter_morgan"
      })
    end

    ...

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

Upvotes: 3

Views: 3167

Answers (1)

d11wtq
d11wtq

Reputation: 35298

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

Upvotes: 5

Related Questions