User7354632781
User7354632781

Reputation: 2274

Rspec: testing model class method

I am trying to test the model class method. here's how model looks like

class Abc
  class Dbc < ActiveRecord::Base
    self.table_name = 'vSomeView'

    def self.class_method(user_id)
      Dbc
      .select('vSomeView.column')
      .where("vSomeView.UserID = #{user_id}")
      .first
    end
  end
end

factory

FactoryGirl.define do
  factory :dbc, class: Abc::Dbc do
  column { 'value' }
  ...
  end
end

Rspec

RSpec.describe Abc::Dbc, type: :model do
  let(:user) { create(:user) }
  let(:dbc) { build_stubbed(:dbc, user: user) }

  describe '.class_method' do
    it 'returns column value' do
      response = dbc.class_method(user.user_id)
      expect(response.column). to eq('value')
    end
  end
end

But i am getting error class_method when i run rspec. What is it i am doing wrong?

Upvotes: 0

Views: 1141

Answers (1)

Edmund Lee
Edmund Lee

Reputation: 2584

When you use factory girl to build an instance, it will build you an instance instantiated from that class.

What you want is

Abc::Dbc.class_method(...)

Upvotes: 1

Related Questions