JohanaAnez
JohanaAnez

Reputation: 47

Ruby 2.2.0 DataMapper: How to test numeric fields

I am working on Ruby 2.2.0, I have a class

class JobOffer
  include DataMapper::Resource

  property :id, Serial
  property :title, String
  property :location, String
  property :experience, Numeric, :default => 0
  property :description, String
  property :created_on, Date
  property :updated_on, Date
  property :is_active, Boolean, :default => true
  belongs_to :user

  validates_presence_of :title
  validates_numericality_of :experience => {
      :greater_than_or_equal_to => 0,
      :less_than_or_equal_to    => 16
  }
end

My question is: How I can test values of experience field in rspec tests? It's easy to test title, because I ask for valid? and it returns if title is present or not, but I don't know if is there something like this for numeric fields. Thanks in advance!

Upvotes: 0

Views: 40

Answers (1)

Paul Byrne
Paul Byrne

Reputation: 1615

Just testing that a an attr can be written for a given model isn't very useful as it's part of the core functionality of the ORM, and likewise with the validators, but if you really want to, you could test creation of a new job offer, something like...

RSpec.describe JobOffer do
 describe ".new" do
   subject(:new) { JobOffer.new(experience: experience_in_years)}
   context "when intialized with experience of 5"
   let(:experience_in_years) { 5 }
   it "has an experience of 5" do
     expect(new.experience).to eq(5) 
   end
   end
 end
end

But again, this is pretty much core functionality of the ORM, so it's arguably not a useful test.

Upvotes: 0

Related Questions