Ben Ben - 3 months ago 17
Ruby Question

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

Answer

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