Skip to content

Commit cf3f04c

Browse files
committed
Define primary_key method on class generated by mock_model("WithAString").
- Closes rspec#435.
1 parent c7388eb commit cf3f04c

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
fixure_file_upload reads from ActiveSupport::TestCase.fixture_path and misses
1919
RSpec's configuration (David Chelimsky)
2020
* Support nested resource in view spec generator (David Chelimsky)
21+
* Define `primary_key` on class generated by mock_model("WithAString") (David
22+
Chelimsky)
2123

2224
### 2.6.1 / 2011-05-25
2325

lib/rspec/rails/mocks.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def mock_model(string_or_model_class, stubs = {})
6363
else
6464
model_class = Object.const_set(string_or_model_class, Class.new do
6565
extend ActiveModel::Naming
66+
def self.primary_key; :id; end
6667
end)
6768
end
6869
else

spec/rspec/rails/mocks/mock_model_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@
101101
end
102102
end
103103

104+
describe "as association that doesn't exist yet" do
105+
before(:each) do
106+
@real = AssociatedModel.create!
107+
@mock_model = mock_model("Other")
108+
@real.nonexistent_model = @mock_model
109+
end
110+
111+
it "passes: associated_model == mock" do
112+
@mock_model.should == @real.nonexistent_model
113+
end
114+
115+
it "passes: mock == associated_model" do
116+
@real.nonexistent_model.should == @mock_model
117+
end
118+
end
119+
104120
describe "#is_a?" do
105121
before(:each) do
106122
@model = mock_model(SubMockableModel)

spec/support/ar_classes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class SubMockableModel < MockableModel
3030
class AssociatedModel < ActiveRecord::Base
3131
extend Connections
3232
belongs_to :mockable_model
33+
belongs_to :nonexistent_model, :class_name => "Other"
3334
end
3435

3536
class AlternatePrimaryKeyModel < ActiveRecord::Base

0 commit comments

Comments
 (0)