Skip to content

Instantly share code, notes, and snippets.

@tbuehlmann
Last active February 13, 2021 15:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tbuehlmann/e114203091be8d18d2c58c7a4debf03d to your computer and use it in GitHub Desktop.
Save tbuehlmann/e114203091be8d18d2c58c7a4debf03d to your computer and use it in GitHub Desktop.
Flaky!
class Comment < ActiveRecord::Base
belongs_to :post, counter_cache: true
end
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem 'activerecord', '6.1.1'
gem 'sqlite3'
gem 'rspec'
gem 'pry'
end
require 'active_record'
require 'rspec'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.integer :comments_count, default: 0, null: false
end
create_table :comments, force: true do |t|
t.belongs_to :post
end
end
autoload :Post, './post.rb'
autoload :Comment, './comment.rb'
RSpec.describe Post do
it 'updates something, whatever' do
post = Post.create!
post.update!(comments_count: 42)
expect(post.reload.comments_count).to eq(42)
end
it 'doesnt really do anything' do
expect(Comment.count).to eq(0)
end
end
RSpec::Core::Runner.invoke
class Post < ActiveRecord::Base
has_many :comments
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment