Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec Rails - Name should be valid - some clarifications

i'm into rspec these days, trying to make my models more precise and accurate. Some things are still a bit weird to me about rspec and so i thought it would be nice if someone could clarify.

Let's say that i have a User model. This one has a :name. The name should be between 4..15 characters(that's a secondary objective, at first it must just exist). So now i'm thinking: What is the best way to test that in a manner that assures that this will happen. To test that a user must have a name, i wrote something like this :

describe User do
    let(:user) { User.new(:name => 'lele') }

    it "is not valid without a name" do
        user.name.should == 'lele'
    end
end

Now, i'm not quite sure that this accomplishes exactly what i want. It seems to me that i'm actually testing Rails with this one. Moreover, if i want to check that a name cannot be more than 15 chars and less than 4, how can this be integrated ?

EDIT:

Maybe this is better ?

describe User do
    let(:user) { User.new(:name => 'lele') }

    it "is not valid without a name" do
        user.name.should_not be_empty
    end

end
like image 338
Spyros Avatar asked Feb 22 '11 21:02

Spyros


1 Answers

You're probably looking for the be_valid matcher:

describe User do
  let(:user) { User.new(:name => 'lele') }

  it "is valid with a name" do
    user.should be_valid
  end

  it "is not valid without a name" do
    user.name = nil
    user.should_not be_valid
  end
end
like image 151
Dylan Markow Avatar answered Oct 04 '22 15:10

Dylan Markow