Skip to content

Commit 6673f77

Browse files
myronmarstonsebjacobs
authored andcommitted
Consistently use record.new_record?.
`be_a_new(model_class)` and `be_new_record` were inconsistent in that one of them checked `!persisted?` and one checked `new_record?`. The docs also said that both checked `!persisted?` even only one did. This changes both to use `new_record?` and to be documented as such. Fixes rspec#1801.
1 parent 1780b0a commit 6673f77

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

lib/rspec/rails/matchers/be_a_new.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ def unmatched_attributes
6161
end
6262

6363
# @api public
64-
# Passes if actual is an instance of `model_class` and returns `false` for
65-
# `persisted?`. Typically used to specify instance variables assigned to
64+
# Passes if actual is an instance of `model_class` and returns `true` for
65+
# `new_record?`. Typically used to specify instance variables assigned to
6666
# views by controller actions
6767
#
6868
# Use the `with` method to specify the specific attributes to match on the

lib/rspec/rails/matchers/be_new_record.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Matchers
44
# @private
55
class BeANewRecord < RSpec::Matchers::BuiltIn::BaseMatcher
66
def matches?(actual)
7-
!actual.persisted?
7+
actual.new_record?
88
end
99

1010
def failure_message
@@ -17,7 +17,7 @@ def failure_message_when_negated
1717
end
1818

1919
# @api public
20-
# Passes if actual returns `false` for `persisted?`.
20+
# Passes if actual returns `true` for `new_record?`.
2121
#
2222
# @example
2323
# get :new

spec/rspec/rails/matchers/be_new_record_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require "spec_helper"
22

33
describe "be_new_record" do
4-
context "un-persisted record" do
5-
let(:record) { double('record', :persisted? => false) }
4+
context "a new record" do
5+
let(:record) { double('record', :new_record? => true) }
66

77
it "passes" do
88
expect(record).to be_new_record
@@ -15,8 +15,8 @@
1515
end
1616
end
1717

18-
context "persisted record" do
19-
let(:record) { double('record', :persisted? => true) }
18+
context "a persisted record" do
19+
let(:record) { double('record', :new_record? => false) }
2020

2121
it "fails" do
2222
expect(record).not_to be_new_record

0 commit comments

Comments
 (0)