I'm learning rspec, I try to valitade length of my model, but I have this errors and I don't know what is going wrong?
#factories/user
FactoryGirl.define do
factory :user do
first_name {"Name"}
last_name {"Surename"}
phone_number {"1245767"}
email {"email#email.net"}
password {"password"}
end
end
user_spec: it { should validate_length_of(:phone_number).is_at_least(7)}
user model: validates_length_of :phone_number, minimum: 7
error:
User validations should ensure phone_number has a length of at least 7
Failure/Error: it { should validate_length_of(:phone_number).is_at_least(7)}
Did not expect errors to include "is too short (minimum is 7 characters)" when phone_number is set to "xxxxxxx",
got errors:
* "can't be blank" (attribute: email, value: "")
* "can't be blank" (attribute: password, value: nil)
* "is too short (minimum is 7 characters)" (attribute: phone_number, value: 0)
* "can't be blank" (attribute: first_name, value: nil)
* "can't be blank" (attribute: last_name, value: nil)
thank you
#edit
require 'rails_helper'
describe User do
describe 'validations' do
it { should validate_presence_of :first_name }
it { should validate_presence_of :last_name }
it { should validate_presence_of :phone_number }
it { should validate_length_of(:phone_number).is_at_least(7)}
end
end
When using an implicit subject, RSpec instantiates the class for you, e.g.:
describe User
it { should_blah_blah }
end
At this point the subject (meaning the thing referred to by it) is an instance of User created by calling User.new. This is convenient for some unit tests, but in your case you want to initialize the user with a factory. Use the explicit subject, e.g.:
describe User
subject { build(:user) }
it { should_blah_blah }
end
Now the subject is a User instance initialized from the factory definition.
More info in the RSpec docs
Related
I have fairly simple model but I'm getting RSpec failures that I cannot find the solution to.
Model -
store_accessor :properties, :fields
store_accessor :priorities
belongs_to :user
belongs_to :external_service
validates :user, :external_service, presence: true
validate :service_enabled?
validates_presence_of :properties, message => "This field is non editable"
Rspec -
require 'rails_helper'
module Application
describe Integration, type: :model do
it { should validate_presence_of(:user) }
it { should validate_presence_of(:external_service) }
it { should validate_presence_of(:properties) }
context 'external service' do
let(:service) { Application::ExternalService.new }
before do
allow(subject).to receive(:external_service).and_return(service)
end
end
end
end
This is the failure that I am getting:
Failure -
Application::Integration should require properties to be set
Failure/Error: it { should validate_presence_of(:properties) }
Expected errors to include "can't be blank" when properties is set to nil,
got errors:
* "can't be blank" (attribute: user, value: nil)
* "can't be blank" (attribute: external_service, value: nil)
* "The service must be enabled to add an integration." (attribute: external_service, value: nil)
* "This field is non editable" (attribute: properties, value: nil)
By default validates_presence_of consider error message as can't be blank. but, as you are setting custom validation message then, you have to verify the same using with_message in spec
it { should validate_presence_of(:properties).with_message('This field is non editable') }
Example for validate_presence_of with with_message
I'm using rails4, factory_ girl, rspec and shoulda matchers. If I run rspec with the code below I get this error:
Product should validate that :website cannot be empty/falsy, producing a custom validation error on failure
Failure/Error: self.website = "http://#{self.website}" unless self.website[/^https?/]
NoMethodError:
undefined method `[]' for nil:NilClass
If I delete unless self.website[/^https?/] from the format_website method I get this error:
Product did not properly validate that :website cannot be empty/falsy,
producing a custom validation error on failure.
After setting :website to ‹nil› -- which was read back as ‹"http://"›
-- the matcher expected the Product to be invalid and to produce a
validation error matching ‹/can't be blank/› on :website. The record
was indeed invalid, but it produced these validation errors instead:
* user: ["can't be blank"]
* name: ["can't be blank"]
* company: ["can't be blank"]
What should I do to make this work?
product model
belongs_to :user
validates :name, presence: { message: "can't be blank" }, length: { maximum: 140, message: "can't be longer than 140 characters" }, uniqueness: { message: "already exists" }
validates :company, presence: { message: "can't be blank" }, length: { maximum: 140, message: "can't be longer than 140 characters" }
validates :website, presence: { message: "can't be blank" }, length: { maximum: 140, message: "can't be longer than 140 characters" }
before_validation :format_website
validate :website_validator
def format_website
self.website = "http://#{self.website}" unless self.website[/^https?/]
end
def website_validator
self.errors.add :website, "format is invalid!" unless website_valid?
end
def website_valid?
!!website.match(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-=\?]*)*\/?$/)
end
factory
FactoryGirl.define do
factory :product do
name { Faker::Commerce.product_name }
company { Faker::Company.name }
website { 'https://example.com' }
user
end
end
it { is_expected.to callback(:format_website).before(:validation) } #this one is not important, if I take out it still gives the same error
it { is_expected.to validate_presence_of(:name).with_message(/can't be blank/) }
it { is_expected.to validate_presence_of(:company).with_message(/can't be blank/) }
it { is_expected.to validate_presence_of(:website).with_message(/can't be blank/) }
it { is_expected.to belong_to(:user) }
You should ensure that website is not nil.
def format_website
return if website.blank?
self.website = "http://#{self.website}" unless self.website[/^https?/]
end
In that case, if self.website == nil, you'll try to call the method [] on a nil object, hence the first error.
For the second case, the answer is in the return you have from rspec:
After setting :website to ‹nil› -- which was read back as ‹"http://"›
Your method format_website returns "http://", which is because website is nil.
I have been creating an application based on the Hartl course, and have added in the concept of Organization, which has_many users. The tests are all the standard tests Hartl recommends up to section 9.2 of the guide book. Since implementing Organizations into the application, one of the test cases is failing "when email address is already taken" - this should block a user from signing up with the same email address twice. What is odd is the fact that this is working in the application itself (form error - "users email address is already taken" thrown) but not in my tests. Can you help and indicate why this has broken please?
User code:
class User < ActiveRecord::Base
belongs_to :organization
#accepts_nested_attributes_for :organization
before_save { self.email = email.downcase }
before_create :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }
validates :organization, presence: true
Organization code:
class Organization < ActiveRecord::Base
validates :organization_name, presence: true, length: { maximum: 50 }, uniqueness: true
has_many :users, :inverse_of => :organization
accepts_nested_attributes_for :users
User spec:
require 'spec_helper'
describe User do
before do
#user = FactoryGirl.create(:user)
end
subject { #user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:remember_token) }
it { should respond_to(:authenticate) }
it { should be_valid }
...
describe "when email address is already taken" do
before do
user_with_same_email = #user.dup
user_with_same_email.email = #user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
Factory Girl Code:
FactoryGirl.define do
factory :organization do
organization_name "Example Org"
trait :wrong do
organization_name "Wrong Org"
end
trait :also_wrong do
organization_name "Another Wrong Org"
end
end
factory :user do
association :organization
name "Example Name"
email "email#example.com"
password "foobar"
password_confirmation "foobar"
trait :wrong_org do
association :organization, :factory => [:organization, :wrong]
end
trait :wrong_org2 do
association :organization, :factory => [:organization, :also_wrong]
end
end
end
The error thrown from the Rails console is as follows:
1) User when email address is already taken should not be valid
Failure/Error: it { should_not be_valid }
expected #<User id: 5287, name: "Example Name", email: "email#example.com", created_at: "2014-07-22 15:04:33", updated_at: "2014-07-22 15:04:33", password_digest: "$2a$04$jrxyuz9e574BoaAhZm6xkOUeAY5spyDut2CCEvAykMu...", organization_id: 5025, remember_token: "339dfafcac7bc5925dbf4e44f60a782f3bbbaa1b">.valid? to return false, got true
I've tried changing the code inside the test, but no matter what I do it keeps throwing an error. As mentioned above, when I open up the application in my local server I can use all functions, and when I try to sign up using a duplicate email address it won't let me. What's wrong with my test code?
The #user is completely valid:
You create your subject, #user. This is valid
You create the user_with_same_email
That user is not valid, because it has the same email as #user
Saving user_with_same_email returns false, but that is not checked in your test
The duplicate user is not saved in the db
The original user is still valid
A correct test would just #dupthe user (or make a new one with the same email), and then check that the new record is not valid.
In chapter 6.x of the Ruby on Rails tutorial by Michael Hartl, I can't get Rspec to pass the email is not present check.
From my limited knowledge:
the User_spec creates a test user using the code after before do.
This user is then checked against the attributes in user.rb to make
sure the :presence is valid.
The check then returns true or false if its valid.
In the next code, before { #user.email = " " } sets the email to
empty
then says it { should_not be_valid }
However, it fails User when email is not present error.
/spec/models/User_spec.rb
require 'spec_helper'
describe User do
before do
#user = User.new(name: "Example User", email: "user#example.com")
end
subject { #user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should be_valid }
describe "when email is not present" do
before { #user.email = " " }
it { should_not be_valid }
end
end
spec/models/user.rb
class User < ActiveRecord::Base
validates :name, presence: true
validates :email, presence: true
end
Failures:
1) User when email is not present
Failure/Error: it { should_not be_valid }
expected # not to be valid
# ./spec/models/user_spec.rb:18:in `block (3 levels) in '
Finished in 0.03278 seconds
4 examples, 1 failure
Failed examples:
rspec ./spec/models/user_spec.rb:18 # User when email is not present
You're validating the email as being present, which means that it would fail if the value is nil or an empty string.
In your rspec block that describes the case where the email should not be present, you're defining the email address as a string consisting of a single space. You actually want to make that a blank string, by removing the space:
before { #user.email = "" }
That will now fail the validation.
I have the following classes in Rails and am writing some rspec tests (any critiques are more than welcome as I'm a nOOb at rspec).
class User.rb
class User < ActiveRecord::Base
email_regex = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, :presence => true ,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => true },
:on => :create
end
and in factories.rb
FactoryGirl.define do
factory :user do
sequence(:name) { |n| "my-name#{n}" }
sequence(:email) { |n| "blue#{n}#12blue.com" }
end
end
and in my rspec (users_spec.rb):
require 'spec_helper'
describe User do
let(:user) { FactoryGirl.build(:user) }
it { user.should be_valid }
it { user.should be_a(User) }
it { user.should respond_to(:email) }
it { user.email = " " }
it { user.should_not be_valid } # this is causing the error
end
and get
1) User
Failure/Error: it { user.should_not be_valid }
expected valid? to return false, got true
But based upon the validates, user should be not be valid. What is going on here? What am I not getting (and I know it's my fault)?
thx
I assume that the test failure surprises you because you think the user email should be " ".
In rspec every example is independent. This means that anything you did in a previous example is forgotten.
In your case your second to last example runs, builds a new, valid activerecord user whose email is "blue4#12blue.com", overwrites that email with " " and then passes since it makes no assertions.
Then your last example runs, builds a new, valid activerecord user who's email is "blue5#12blue.com" and fails because the user is valid, it's email has not been overwritten.
You probably want something like this:
it 'should validate the email' do
user.email = " "
user.should_not be_valid
end