Rspec Rails - the name must be valid - some clarifications

I'm at rspec right now trying to make my models more accurate and accurate. Some things are still a little weird for me about rspec, and so I thought it would be nice if someone could clarify.

Say I have a User model. This one has: a name. The name should be between 4..15 characters (this is a secondary goal, at first it should simply exist). So now I think: what is the best way to test this so that it happens. To verify that the 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 is exactly what I want. It seems to me that I'm actually testing Rails with this. Moreover, if I want to check that the name cannot be more than 15 characters 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 
+6
ruby-on-rails rspec
source share
4 answers

I am using this method:

 describe User do it "should have name" do lambda{User.create! :name => nil}.should raise_error end it "is not valid when the name is longer than 15 characters" do lambda{User.create! :name => "im a very looooooooong name"}.should raise_error end it "is not valid when the name is shorter than 4 characters" do lambda{User.create! :name => "Tom"}.should raise_error end end 
+5
source share

You are probably looking for the be_valid subdirectory:

 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 
+15
source share

I like to check the actual error messages to check:

 require 'spec_helper' describe User do let (:user) { User.new } it "is invalid without a name" do user.valid? user.errors[:name].should include("can't be blank") end it "is invalid when less than 4 characters" do user.name = "Foo" user.valid? user.errors[:name].should include("is too short (minimum is 4 characters)") end it "is invalid when greater than 15 characters" do user.name = "A very, very, very long name" user.valid? user.errors[:name].should include("is too long (maximum is 15 characters)") end end 

It is also useful to use a factory that creates an object with valid attributes, which you can invalidate one at a time for testing.

+2
source share

I would use something like this

 class User < ActiveRecord::Base validates_presence_of :name validates_length_of :name, :in => 4..15 end describe User do it "validates presence of name" do user = User.new user.valid?.should be_false user.name = "valid name" user.valid?.should be_true end it "validates length of name in 4..15" do user = User.new user.name = "123" user.valid?.should be_false user.name = "1234567890123456" user.valid?.should be_false user.name = "valid name" user.valid?.should be_true end end 

Most notably, I use active record checks for both conditions. In my examples, I do not rely on error strings. In the examples that test the behavior of validations, there is no reason to touch the database, so I do not. In each example, I test the behavior of an object when it is valid and invalid.

0
source share

All Articles