Skip to content

Instantly share code, notes, and snippets.

@nmk
Last active December 14, 2015 07:29
Show Gist options
  • Save nmk/5050985 to your computer and use it in GitHub Desktop.
Save nmk/5050985 to your computer and use it in GitHub Desktop.
Testing inverse of
ruby "2.0.0"
source "https://rubygems.org"
gem "activerecord", "4.0.0.beta1"
gem "sqlite3"
gem "rspec"
require "bundler"
Bundler.setup
require "active_record"
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
ActiveRecord::Migration.create_table :posts do |t|
end
ActiveRecord::Migration.create_table :comments do |t|
t.integer :post_id
end
class Post < ActiveRecord::Base
has_many :comments, inverse_of: :post
end
class Comment < ActiveRecord::Base
belongs_to :post
end
describe "inverse_of option" do
let!(:post) { Post.create! }
let!(:comment) { Comment.create!(post: post) }
it "should work when calling #association#first" do
expect(post.comments.first.post).to equal post
end
it "should work when calling #association#find" do
expect(post.comments.find(comment.id).post).to equal post
end
end
inverse-of $ bundle install
Using rake (10.0.3)
Using i18n (0.6.4)
Using minitest (4.6.2)
Using multi_json (1.6.1)
Using atomic (1.0.1)
Using thread_safe (0.1.0)
Using tzinfo (0.3.35)
Using activesupport (4.0.0.beta1) from git://github.com/wangjohn/rails.git (at 2e41e84)
Using builder (3.1.4)
Using erubis (2.7.0)
Using rack (1.5.2)
Using rack-test (0.6.2)
Using actionpack (4.0.0.beta1) from git://github.com/wangjohn/rails.git (at 2e41e84)
Using mime-types (1.21)
Using polyglot (0.3.3)
Using treetop (1.4.12)
Using mail (2.5.3)
Using actionmailer (4.0.0.beta1) from git://github.com/wangjohn/rails.git (at 2e41e84)
Using activemodel (4.0.0.beta1) from git://github.com/wangjohn/rails.git (at 2e41e84)
Using activerecord-deprecated_finders (0.0.3)
Using arel (4.0.0.beta1)
Using activerecord (4.0.0.beta1) from git://github.com/wangjohn/rails.git (at 2e41e84)
Using diff-lcs (1.2.1)
Using hike (1.2.1)
Using json (1.7.7)
Using bundler (1.3.0)
Using rdoc (3.12.2)
Using thor (0.17.0)
Using railties (4.0.0.beta1) from git://github.com/wangjohn/rails.git (at 2e41e84)
Using tilt (1.3.4)
Using sprockets (2.9.0)
Using sprockets-rails (2.0.0.rc3)
Using rails (4.0.0.beta1) from git://github.com/wangjohn/rails.git (at 2e41e84)
Using rspec-core (2.13.0)
Using rspec-expectations (2.13.0)
Using rspec-mocks (2.13.0)
Using rspec (2.13.0)
Using sqlite3 (1.3.7)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
inverse-of $ bundle exec rspec inverse_of_spec.rb -- INSERT --
-- create_table(:posts)
-> 0.0070s
-- create_table(:comments)
-> 0.0004s
.F
Failures:
1) inverse_of option should work when calling #association#find
Failure/Error: expect(post.comments.where(id: comment.id).first.post).to equal post
expected #<Post:70241628841300> => #<Post id: 2>
got #<Post:70241629084820> => #<Post id: 2>
Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
`expect(actual).to eq(expected)` if you don't care about
object identity in this example.
Diff:
# ./inverse_of_spec.rb:33:in `block (2 levels) in <top (required)>'
Finished in 0.04367 seconds
2 examples, 1 failure
Failed examples:
rspec ./inverse_of_spec.rb:31 # inverse_of option should work when calling #association#find
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment