ruby on rails - Rspec test readonly model -


i have small aplication in rails 5. tried use rspec test failed readonly model.

i have readonly model product , normal model productmsp. second 1 list of prices associated product , stored in rails db. product external readonly connection database withdraw list of products.

i added association product spec/factories/product_msps.rb.

factorygirl.define   factory :product_msp     product_id 11     initial_date '01-01-2017'     expiry_date '31-01-2017'     msp 9999.99     association :product   end end 

i didn't add new test. when run rspec once again failures. rspec want create products, cannot due readonly database table.

failures:     1) productmsp validations initial_date cannot empty     failure/error: pm = factorygirl.build(:product_msp, initial_date: nil)             activerecord::readonlyrecord:             product marked readonly                  # ./spec/models/product_msp_spec.rb:17:in `block (3 levels) in <top (required)>' 

below failing test: (there more failures)

require 'rails_helper'  rspec.describe productmsp, type: :model     'initial_date cannot empty'       pm = factorygirl.build(:product_msp, initial_date: nil)       pm.valid?       expect(pm.errors[:initial_date]).to include("can't blank")     end end 

spec/factories/products.rb

factorygirl.define   factory :product     id 11     name 'porsche cayenne'   end end 

and app/model/product.rb

class product < applicationrecord   self.primary_key = 'tw_id'    def readonly?     true   end    has_many :product_msps    default_scope {where(product_type:1).order(:name)} 

a main question is: how deal test , readonly models?

  • is rspec suitable it?

  • should rid of testing readonly models

i searched through internet didn't find examples of such problem. need build app on top of database, have tested somehow. :-)

(for future searchers:) )

you should add skip_create factory read-only model. allows avoiding database record creation , should solve problem this. useful if want create factory class, not connected database (does not inherited `activerecord::base).


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -