Question

So I have been racking my brain at this and maybe some of you might have a better idea on how to do proper unit test for this User model. My basic unit test looks like this.

test "should not save without name" do
  user = User.new
  user.email = "test@test.com"
  user.password = "letmein"
  assert !user.save
end

This test passes with this model.

class User < ActiveRecord::Base
  include Clearance::User

  validates :name, presence: true

  has_and_belongs_to_many :contests
end

Is there a better way to do this in Clearance? It is nice the gem lets you create users like this on the fly by arbitrarily assigning email and password but I'm thinking maybe I shouldn't have to do this.

Was it helpful?

Solution

user = User.new(:email => "test@test.com", :password => "letmein")

and then,

assert !user.valid?

or

user.should_not be_valid

or

expect { user.save }.to change(User, :count).by(0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top