0

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!

JohanaAnez
  • 31
  • 5

1 Answers1

0

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.

Paul Byrne
  • 1,563
  • 18
  • 24
  • I might add that this library is no longer maintained and I would recommend choosing ActiveRecord or Sequel instead, the latter if you wish to avoid Railsland for whatever reason – Paul Byrne May 06 '18 at 17:56