Rails Rspec allow_any_instance_of raises "does not implement" error for ActiveRecord object attributes -


i have next problem. when try stub instance method of activerecord model allow_any_instance_of receive error message "model not implement #method", if send real request database create or select instance object of model before stub, don't have message , ok! have same issue in console

reloading...  >> search.method_defined?(:tickets_count) => false  >> search.method_defined?(:affiliate_id) => false  >> search.last   search load (3.9ms)  select  "searches".* "searches"  order "searches"."id" desc limit 1 => #<search:0x007fe2aecf2900  id: 515711,  tickets_count: 1,  affiliate_id: nil  >> search.method_defined?(:affiliate_id) => true  >> search.method_defined?(:tickets_count) => true 

could explain, hell going on, please?))

what you're running here lazy-instantiation of database attribute accessor methods activerecord.

tl;dr can use https://github.com/rspec/rspec-rails/issues/1357

essentially activerecord::base classes don't define of database-column based setters/getters until need to, typically through method-missing hook, though there may other things cause them become defined, example calling search.find_by_affiliate_id may cause definitions load.

i can provide high level explanation of how/why works way does, there better / more up-to-date resources can better explain it, or can read through activerecord source code which, while bit obtuse, can reasonable option understanding behavior.

so why aren't these methods defined?

when new activerecord class created inheriting activerecord::base, class knows it's expected table name, doesn't , couldn't know columns table has without connecting database. without knowing columns exist, couldn't know readers/writers define. activerecord not proactively query database @ load time, think thing, lets load code without requiring migrated, connected database.

what activerecord hook method_missing, responds_to?, etc. determine when method being called on activerecord class not defined, attempting load current db schema , define reader/writer/accessor methods on each column finds, retrying method call see if it's defined.

in example above, it's not .last call defining attributes, rather .inspect call terminal runs in order print instance of search object.

to test this, re-ran example above replace search.last (s = search.last).nil?. if did instead, bet search.method_defined?(:affiliate_id) still false.

hope explains why methods not defined @ first, later on. reason why couldn't use alias_method :aliased_affiliate_id, :affiliate_id, fail because there's no method affiliat_id defined @ load time.

for you're trying do, need decide if it's worth require active database connection in order run these specs.

if so, can trigger activerecord classes load table definitions , define reader/writer attributes in specs in order make pass.

here's running same issue rspec monkey-patch solution loads definitions activerecord classes:

https://github.com/rspec/rspec-rails/issues/1357


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -