Skip to content

Commit

Permalink
Define primary_key method on class generated by mock_model("WithAStri…
Browse files Browse the repository at this point in the history
…ng").

- Closes rspec#435.
  • Loading branch information
dchelimsky committed Sep 6, 2011
1 parent c7388eb commit cf3f04c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
fixure_file_upload reads from ActiveSupport::TestCase.fixture_path and misses
RSpec's configuration (David Chelimsky)
* Support nested resource in view spec generator (David Chelimsky)
* Define `primary_key` on class generated by mock_model("WithAString") (David
Chelimsky)

### 2.6.1 / 2011-05-25

Expand Down
1 change: 1 addition & 0 deletions lib/rspec/rails/mocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def mock_model(string_or_model_class, stubs = {})
else
model_class = Object.const_set(string_or_model_class, Class.new do
extend ActiveModel::Naming
def self.primary_key; :id; end
end)
end
else
Expand Down
16 changes: 16 additions & 0 deletions spec/rspec/rails/mocks/mock_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@
end
end

describe "as association that doesn't exist yet" do
before(:each) do
@real = AssociatedModel.create!
@mock_model = mock_model("Other")
@real.nonexistent_model = @mock_model
end

it "passes: associated_model == mock" do
@mock_model.should == @real.nonexistent_model
end

it "passes: mock == associated_model" do
@real.nonexistent_model.should == @mock_model
end
end

describe "#is_a?" do
before(:each) do
@model = mock_model(SubMockableModel)
Expand Down
1 change: 1 addition & 0 deletions spec/support/ar_classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class SubMockableModel < MockableModel
class AssociatedModel < ActiveRecord::Base
extend Connections
belongs_to :mockable_model
belongs_to :nonexistent_model, :class_name => "Other"
end

class AlternatePrimaryKeyModel < ActiveRecord::Base
Expand Down

0 comments on commit cf3f04c

Please sign in to comment.