Last active
December 3, 2018 13:02
-
-
Save AstmDesign/3bda847560423ecd852b394158e7290c to your computer and use it in GitHub Desktop.
Created simple Rails Rspec testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rails_helper" | |
RSpec.describe User, :type => :model do | |
before(:each) do | |
@user = User.create!(first_name: "Astm", last_name: "Ali") | |
end | |
# Creating validation | |
describe "Creating" do | |
it "We should have now new user created" do | |
expect(User.all.count).to eq(1) | |
end | |
end | |
# Require fields validation | |
describe "Require validations" do | |
it "first name must be require" do | |
@user.first_name = nil | |
expect(@user).to_not be_valid | |
end | |
it "last name must be require" do | |
@user.last_name = nil | |
expect(@user).to_not be_valid | |
end | |
end | |
# Uniqueness fields validation | |
describe "Uniqueness validations" do | |
it "first name must be unique" do | |
should validate_uniqueness_of(:first_name) | |
end | |
it "last name must be unique" do | |
should validate_uniqueness_of(:last_name) | |
end | |
end | |
# Length fields validation | |
describe "Length validations" do | |
it "first name must be less than 20 characters" do | |
@user.first_name = "A" * 21 | |
expect(@user).to_not be_valid | |
end | |
it "last name must be less than 20 characters" do | |
@user.last_name = "A" * 21 | |
expect(@user).to_not be_valid | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment