-
Notifications
You must be signed in to change notification settings - Fork 0
/
AR_dup_and_rspec_change.rb
67 lines (51 loc) · 1.41 KB
/
AR_dup_and_rspec_change.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem 'activerecord', '5.2.3'
gem 'sqlite3', '1.4.1'
gem 'rspec', '3.8.0'
gem 'pry'
end
require 'active_record'
require 'pry'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Migration.create_table(:users)
ActiveRecord::Migration.create_table(:roles) do |t|
t.integer :user_id
end
class Role < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :roles
end
# user = User.new
# binding.pry
# roles = user.roles.build
# binding.pry
# duplicated = user.roles.dup
# binding.pry
RSpec.describe 'change matcher' do
let(:user) { User.new }
let(:action) { ->{ user.roles.build } }
let(:assert_before) { ->{ be_empty } }
let(:assert_after) { ->{ contain_exactly(an_instance_of(Role)) } }
it 'demonstrates change works properly' do
sum = 0
expect{ sum += 1 }.to change { sum }.from(0).to(1)
end
it 'works when tested without change syntax' do
expect(user.roles).to(assert_before.call)
action.call
expect(user.roles).to(assert_after.call)
end
it 'works when tested with change syntax' do
expect{ action.call }
.to change{ user.roles }
.from(assert_before.call)
.to(assert_after.call)
end
end
RSpec::Core::Runner.invoke