Skip to content

Instantly share code, notes, and snippets.

@miyohide
Created May 17, 2014 11:16
Show Gist options
  • Save miyohide/cb7f09ef24c1fac412d3 to your computer and use it in GitHub Desktop.
Save miyohide/cb7f09ef24c1fac412d3 to your computer and use it in GitHub Desktop.
# itの中に puts @smith を加えると、それぞれのitのなかで返ってくる @smith オブジェクトは異なる。
# ただし、puts @smith.inspectとするとそれぞれが同じ値を持っていて、同じオブジェクトのように思える。
require 'spec_helper'
describe Contact do
# 省略
describe "filter last name by letter" do
before :each do
@smith = Contact.create(firstname: 'John', lastname: 'Smith',
email: 'jsmith@example.com')
@jones = Contact.create(firstname: 'Tim', lastname: 'Jones',
email: 'tjones@example.com')
@johnson = Contact.create(firstname: 'John', lastname: 'Johnson',
email: 'jjohnson@example.com')
end
context "matching letters" do
it "returns a sorted array of results that match" do
puts @smith #=> #<Contact:0x007fe783380ff0>
puts @smith.inspect #=> #<Contact id: 4, firstname: "John", lastname: "Smith", email: "jsmith@example.com", created_at: "2014-05-17 11:14:12", updated_at: "2014-05-17 11:14:12">
expect(Contact.by_letter("J")).to eq [@johnson, @jones]
end
end
context "non-matching letters" do
it "returns a sorted array of results that match" do
puts @smith #=> #<Contact:0x007fe780bf1f98>
puts @smith.inspect #=> #<Contact id: 4, firstname: "John", lastname: "Smith", email: "jsmith@example.com", created_at: "2014-05-17 11:14:12", updated_at: "2014-05-17 11:14:12">
expect(Contact.by_letter("J")).to_not include @smith
end
end
end
end
@miyohide
Copy link
Author

puts @smith.inspectとして、同じ値が返ってくる理由は、log/test.logをみるとよく分かる。

   (0.1ms)  begin transaction
   (0.1ms)  SAVEPOINT active_record_1
  Contact Exists (0.7ms)  SELECT 1 AS one FROM "contacts" WHERE "contacts"."email" = 'jsmith@example.com' LIMIT 1
  SQL (4.9ms)  INSERT INTO "contacts" ("created_at", "email", "firstname", "lastname", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Sat, 17 May 2014 11:14:12 UTC +00:00], ["email", "jsmith@example.com"], ["firstname", "John"], ["lastname", "Smith"], ["updated_at", Sat, 17 May 2014 11:14:12 UTC +00:00]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.1ms)  SAVEPOINT active_record_1
  Contact Exists (0.1ms)  SELECT 1 AS one FROM "contacts" WHERE "contacts"."email" = 'tjones@example.com' LIMIT 1
  SQL (0.9ms)  INSERT INTO "contacts" ("created_at", "email", "firstname", "lastname", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Sat, 17 May 2014 11:14:12 UTC +00:00], ["email", "tjones@example.com"], ["firstname", "Tim"], ["lastname", "Jones"], ["updated_at", Sat, 17 May 2014 11:14:12 UTC +00:00]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.1ms)  SAVEPOINT active_record_1
  Contact Exists (0.1ms)  SELECT 1 AS one FROM "contacts" WHERE "contacts"."email" = 'jjohnson@example.com' LIMIT 1
  SQL (0.2ms)  INSERT INTO "contacts" ("created_at", "email", "firstname", "lastname", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Sat, 17 May 2014 11:14:12 UTC +00:00], ["email", "jjohnson@example.com"], ["firstname", "John"], ["lastname", "Johnson"], ["updated_at", Sat, 17 May 2014 11:14:12 UTC +00:00]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
  Contact Load (0.3ms)  SELECT "contacts".* FROM "contacts" WHERE (lastname LIKE 'J%') ORDER BY "contacts".lastname ASC
   (2.1ms)  rollback transaction
   (0.1ms)  begin transaction
   (0.1ms)  SAVEPOINT active_record_1
  Contact Exists (0.1ms)  SELECT 1 AS one FROM "contacts" WHERE "contacts"."email" = 'jsmith@example.com' LIMIT 1
  SQL (0.4ms)  INSERT INTO "contacts" ("created_at", "email", "firstname", "lastname", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Sat, 17 May 2014 11:14:12 UTC +00:00], ["email", "jsmith@example.com"], ["firstname", "John"], ["lastname", "Smith"], ["updated_at", Sat, 17 May 2014 11:14:12 UTC +00:00]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.0ms)  SAVEPOINT active_record_1
  Contact Exists (0.4ms)  SELECT 1 AS one FROM "contacts" WHERE "contacts"."email" = 'tjones@example.com' LIMIT 1
  SQL (0.5ms)  INSERT INTO "contacts" ("created_at", "email", "firstname", "lastname", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Sat, 17 May 2014 11:14:12 UTC +00:00], ["email", "tjones@example.com"], ["firstname", "Tim"], ["lastname", "Jones"], ["updated_at", Sat, 17 May 2014 11:14:12 UTC +00:00]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.1ms)  SAVEPOINT active_record_1
  Contact Exists (0.1ms)  SELECT 1 AS one FROM "contacts" WHERE "contacts"."email" = 'jjohnson@example.com' LIMIT 1
  SQL (0.2ms)  INSERT INTO "contacts" ("created_at", "email", "firstname", "lastname", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Sat, 17 May 2014 11:14:12 UTC +00:00], ["email", "jjohnson@example.com"], ["firstname", "John"], ["lastname", "Johnson"], ["updated_at", Sat, 17 May 2014 11:14:12 UTC +00:00]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
  Contact Load (0.2ms)  SELECT "contacts".* FROM "contacts" WHERE (lastname LIKE 'J%') ORDER BY "contacts".lastname ASC
   (0.6ms)  rollback transaction

分かりにくいんですが、begin transactionrollback transactionがそれぞれ2つずつあります。
rollback transactionbegin transactionからrollback transactionの間で起きた変更をすべて元に戻す処理が行われます。このため、idも含め、同じデータが返ってきます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment