简体   繁体   中英

Rails and Rspec Unit Testing Static Methods

I have a very simple static method in one of my models:

def self.default
    self.find(1)
end

I'm trying to write a simple Rspec unit test for it that doesn't make any calls to the DB. How do I write a test that generates a few sample instances for the test to return? Feel free to complete this:

describe ".default" do
    context "when testing the default static method" do
        it "should return the instance where id = 1" do

        end
    end
end

The model file is as follows:

  class Station < ApplicationRecord
  acts_as_paranoid
  acts_as_list
  nilify_blanks

  belongs_to :color
  has_many :jobs
  has_many :station_stops
  has_many :logs, -> { where(applicable_class: :Station) }, foreign_key: :applicable_id
  has_many :chattels, -> { where(applicable_class: :Station) }, foreign_key: :applicable_id

  delegate :name, :hex, to: :color, prefix: true

  def name
    "#{full_display} Station"
  end

  def small_display
    display_short || code.try(:titleize)
  end

  def full_display
    display_long || small_display
  end

  def average_time
    Time.at(station_stops.closed.average(:time_lapsed)).utc.strftime("%-M:%S")
  end

  def self.default
    # referencing migrate/create_stations.rb default for jobs
    self.find(1)
  end

  def self.first
    self.where(code: Constant.get('station_code_to_enter_lab')).first
  end
end

The spec file is as follows:

require "rails_helper"
describe Station do

  subject { described_class.new  }

  describe "#name" do
    context "when testing the name method" do
      it "should return the capitalized code with spaces followed by 'Station'" do
        newStation = Station.new(code: 'back_to_school')
        result = newStation.name
        expect(result).to eq 'Back To School Station'
      end
    end
  end

  describe "#small_display" do
    context "when testing the small_display method" do
      it "should return the capitalized code with spaces" do
        newStation = Station.new(code: 'back_to_school')
        result = newStation.small_display
        expect(result).to eq 'Back To School'
      end
    end
  end

  describe "#full_display" do
    context "when testing the full_display method" do
      it "should return the capitalized code with spaces" do
        newStation = Station.new(code: 'back_to_school')
        result = newStation.full_display
        expect(result).to eq 'Back To School'
      end
    end
  end

  describe ".default" do
    context "" do
      it "" do

      end
    end
  end

end

You can use stubbing to get you there

describe ".default" do
    context "when testing the default static method" do
        let(:dummy_station) { Station.new(id: 1) }
        before { allow(Station).to receive(:default).and_return(dummy_station)

        it "should return the instance where id = 1" do
          expect(Station.default.id).to eq 1
        end
    end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM