Permalink
Browse files

set up connections in mock/stub_model specs

- less invasive than stubbing AR internals
  • Loading branch information...
1 parent a94f6f9 commit b8422df3eaaed765b1ecb2bb738ad0ec0917acc6 @dchelimsky dchelimsky committed Jan 11, 2011
Showing with 17 additions and 29 deletions.
  1. +13 −25 spec/rspec/rails/mocks/ar_classes.rb
  2. +4 −4 spec/rspec/rails/mocks/stub_model_spec.rb
@@ -1,14 +1,13 @@
-module NoConnections
- def self.included(mod)
- (class << mod; self; end).class_eval do
- def columns
- []
- end
-
- def connection
- RSpec::Mocks::Mock.new.as_null_object
- end
- end
+module Connections
+ def self.extended(host)
+ host.establish_connection :adapter => 'sqlite3',
+ :database => ':memory:'
+
+ host.connection.execute <<-eosql
+ CREATE TABLE #{host.table_name} (
+ #{host.primary_key} integer PRIMARY KEY AUTOINCREMENT
+ )
+ eosql
end
end
@@ -18,31 +17,20 @@ class NonActiveRecordModel
end
class MockableModel < ActiveRecord::Base
- include NoConnections
+ extend Connections
has_one :associated_model
end
class SubMockableModel < MockableModel
end
class AssociatedModel < ActiveRecord::Base
- include NoConnections
+ extend Connections
belongs_to :mockable_model
end
class AlternatePrimaryKeyModel < ActiveRecord::Base
- include NoConnections
self.primary_key = :my_id
+ extend Connections
attr_accessor :my_id
end
-
-class ConnectableModel < ActiveRecord::Base
- establish_connection :adapter => 'sqlite3',
- :database => ':memory:'
-
- connection.execute <<-eosql
- CREATE TABLE connectable_models (
- id integer PRIMARY KEY AUTOINCREMENT
- )
- eosql
-end
@@ -84,7 +84,7 @@ def model_class
it "raises when hitting the db" do
lambda do
- stub_model(ConnectableModel).connection
+ stub_model(MockableModel).connection
end.should raise_error(RSpec::Rails::IllegalDataAccessException, /stubbed models are not allowed to access the database/)
end
@@ -108,7 +108,7 @@ def model_class
describe "alternate primary key" do
it "has the correct primary_key name" do
- stub_model(AlternatePrimaryKeyModel).class.primary_key.should eql('my_id')
+ stub_model(AlternatePrimaryKeyModel).class.primary_key.to_s.should eql('my_id')
end
it "has a primary_key" do
@@ -138,11 +138,11 @@ def model_class
end
it "passes associated_model == mock" do
- @stub_model.should == @real.mockable_model
+ @stub_model.should == @real.mockable_model
end
it "passes mock == associated_model" do
- @real.mockable_model.should == @stub_model
+ @real.mockable_model.should == @stub_model
end
end

0 comments on commit b8422df

Please sign in to comment.