Rspec check of counter_cache column returning 0

For several days I’ve been trying to understand what a seam should be very light ... I’m still very new to the world of rails and rubies, and I just can’t work on this one of ...: p

In any case, the problem I am facing is that I have several columns: counter_cache in my model, which all work very well when checking them manually. However, I want to do TDD and I cant the seam to check them in rspec for an unknown reason ??

Anyway, here is an example of my model (User's, comments and Media):

class User < ActiveRecord::Base has_many :comments has_many :media, dependent: :destroy end class Comment < ActiveRecord::Base attr_accessible :content, :user_id belongs_to :commentable, polymorphic: true, :counter_cache => true belongs_to :user, :counter_cache => true validates :user_id, :presence => true validates :content, :presence => true, :length => { :maximum => 255 } end class Medium < ActiveRecord::Base attr_accessible :caption, :user_id belongs_to :user, :counter_cache => true has_many :comments, as: :commentable validates :user_id, presence: true validates :caption, length: { maximum: 140 }, allow_nil: true, allow_blank: true default_scope order: 'media.created_at DESC' end 

Here is an example of setting a table schema:

 create_table "users", :force => true do |t| t.integer "comments_count", :default => 0, :null => false t.integer "media_count", :default => 0, :null => false end create_table "comments", :force => true do |t| t.text "content" t.integer "commentable_id" t.string "commentable_type" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false t.integer "user_id" end create_table "media", :force => true do |t| t.integer "user_id" t.string "caption" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false t.integer "comments_count", :default => 0, :null => false end 

And here is an example rspec example that I tried:

 require 'spec_helper' describe "Experimental" do describe "counter_cache" do let!(:user) { FactoryGirl.create(:user)} subject { user } before do @media = user.media.create(caption: "Test media") end its "media array should include the media object" do m = user.media m.each do |e| puts e.caption # Outputting "Test media" as expected end user.media.should include(@media) #This works end it "media_count should == 1 " do # This isnt working? puts user.media_count #Outputting 0 user.media_count.should == 1 end end end 

And finally, the error message that rspec gives me:

 Failures: 1) Experimental counter_cache media_count should == 1 Failure/Error: user.media_count.should == 1 expected: 1 got: 0 (using ==) # ./spec/models/experimental_spec.rb:24:in `block (3 levels) in <top (required)>' Finished in 0.20934 seconds 2 examples, 1 failure 

Also note that this happens for the entire counter_cache column in all my models. I also tried several different ways to test this, but they all return the above error message.

Indeed, hoping someone could help me. :)

Thanks for the heaps in advance! Luke

+6
source share
1 answer

counter_cache directly updated in the database. This will not affect the copy of the model you loaded into memory, so you need to reload it:

 it "media_count should == 1 " do user.reload user.media_count.should == 1 end 

But I do not think that is exactly how I would experience it. Since you have this, your test is very closely related to the setup code, which seems like it shouldn't be there at all. How about something similar for a separate spec:

 it "has a counter cache" do user = FactoryGirl.create(:user) expect { user.media.create(caption: "Test media") }.to change { User.last.media_count }.by(1) end 
+26
source

Source: https://habr.com/ru/post/924026/


All Articles