Skip to content
This repository has been archived by the owner on Mar 14, 2023. It is now read-only.

Test Unit

ZeroC0D3 Team edited this page Jan 9, 2018 · 1 revision

Test Unit

  1. Initialize rspec
rspec --init
  1. Define rspec helper in spec/spec_helper.rb folder

  2. Create test unit for models

RSpec.describe [ClassModel], :type => :model do

  context "associations" do
    it { should belong_to(:field) }
    it { should have_many(:fields) }
  end

  context "validations" do
    should validate_presence_of(:field_name)
  end

  context "#field_name" do
    should "Consist of field_name" do
      class_model = ClassModel.new([field]: [value])
      assert_equal [value], class_model.[field]
    end
  end

end

Example:

RSpec.describe Device, :type => :model do

  context "associations" do
    it { should belong_to(:device_profile) }
    it { should have_many(:group_members) }
    it { should have_many(:device_groups) }
  end

  context "validations" do
    should validate_presence_of(:name)
    should validate_presence_of(:serial)
    should validate_presence_of(:resolution)
    should validate_presence_of(:device_profile_id)
  end

  context "#name" do
    should "Consist of name device" do
      device = Device.new(name: "Pilot 00001")
      assert_equal "Pilot 00001", device.name
    end
  end

  context "#serial" do
    should "Consist of serial device" do
      device = Device.new(serial: "353110080105281")
      assert_equal "353110080105281", device.serial
    end
  end

  context "#resolution" do
    should "Consist of resolution device" do
      device = Device.new(resolution: "1900x1200")
      assert_equal "1900x1200", device.resolution
    end
  end

  context "#device_profile_id" do
    should "Consist of profile_id device" do
      device = Device.new(device_profile_id: 1)
      assert_equal 1, device.device_profile_id
    end
  end
  
end
Clone this wiki locally