mercredi 31 juillet 2019

How to set FactoryBot to be used by controller when test

Situation:

I can set FactoryBot to register 2 models as follow: When rspec it. It only use FactoryBot called by spec file.But the model that called by one in controller not been use through FactoryBot.

My Question is:

How I can test the with related models using FactoryBot like this situation.

 FactoryBot.define do

     factory :MyTestResults do
         errorCode { 100 }
         resultDesc { "" }
     end

     factory :TestCharts do
         errorCode { 100 }
         resultDesc { "This description from FactoryBot " }
     end

 end

Follow is my spec file to be tested. I code up this test just to ask this issue. 1. Is there any other way to fully test using 2 FactoryBot(models) in spec file and controller.


 # ./spec/factories/My_Work_spec.rb
 require "rails_helper"
 describe WorkerGroup do
   workergroup = WorkerGroup.new
   describe "My Work can do" do

     it "To work result dessc should use same FactoryBot class" do
       my_test_result = FactoryBot.build(:MyTestResults)
       test_charts    = FactoryBot.build(:TestCharts)

       @errorCode     = my_test_result.errorCode

       # Expect workergroup call TestChart using FactoryBot
       # If so should pass the test
       # If not it call TestChart directly
       # The Result ====> Not Pass
       # Why workgroup not use / or how to make it use
       # FactoryBot

       myResultDesc      = workergroup.perform(@errorCode)
       chartResultDesc   = test_charts.resultDesc
       expect(myResultDesc).to eq(chartResultDesc)
     end
   end
 end

This is migration script for model

 rails g model My_Test_Result errCode:int resultDesc:varchar

 rails g model Test_chart errCode:int resultDesc:varchar

In controller or workgroup as follow:

 class WorkerGroup < ActiveJob::Base
     queue_as :hign
     def perform(errorCode)
         if (@testcharts = TestCharts.where(errorCode: errorCode).first)
             @chartResultDesc = @testcharts.resultDesc
         else
            @chartResultDesc = ""
         end
         return @chartResultDesc
     end
 end

The RSpec tested result here:

Failures:

1) WorkerGroup My Work can do To work result dessc should use same FactoryBot class Failure/Error: expect(myResultDesc).to eq(chartResultDesc)

   expected: "This description from FactoryBot "
        got: "This is description from database"

   (compared using ==)
 # ./spec/jobs/My_Work_spec.rb:28:in `block (3 levels) in <top (required)>'

Top 1 slowest examples (1.43 seconds, 98.7% of total time):
WorkerGroup My Work can do To work result dessc should use same FactoryBot class 1.43 seconds ./spec/jobs/My_Work_spec.rb:11

Finished in 1.45 seconds (files took 27.47 seconds to load) 1 example, 1 failure

Failed examples:

rspec ./spec/jobs/My_Work_spec.rb:11 # WorkerGroup My Work can do To work result dessc should use same FactoryBot class

Randomized with seed 42268

Aucun commentaire:

Enregistrer un commentaire