Skip to content

Commit 57741c1

Browse files
committed
Merge pull request #5 from rspec/rails_4_0_4
Supports `has_attribute?` on mock_model doubles
2 parents dab34f3 + 47993ba commit 57741c1

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/rspec/active_model/mocks/mocks.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ def self.primary_key; :id; end
146146
model_class.respond_to?(:column_names) && model_class.column_names.include?(method_name.to_s)
147147
end
148148

149+
msingleton.__send__(:define_method, :has_attribute?) do |attr_name|
150+
__model_class_has_column?(attr_name)
151+
end unless stubs.has_key?(:has_attribute?)
152+
149153
msingleton.__send__(:define_method, :respond_to?) do |method_name, *args|
150154
include_private = args.first || false
151155
__model_class_has_column?(method_name) ? true : super(method_name, include_private)

spec/rspec/active_model/mocks/mock_model_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,33 @@
193193
end
194194
end
195195

196+
describe "#has_attribute?" do
197+
context "with an ActiveRecord model" do
198+
before(:each) do
199+
MockableModel.stub(:column_names).and_return(["column_a", "column_b"])
200+
@model = mock_model(MockableModel)
201+
end
202+
203+
it "has a given attribute if the underlying model has column of the same name" do
204+
expect(@model.has_attribute?("column_a")).to be_truthy
205+
expect(@model.has_attribute?("column_b")).to be_truthy
206+
expect(@model.has_attribute?("column_c")).to be_falsey
207+
end
208+
209+
it "accepts symbols" do
210+
expect(@model.has_attribute?(:column_a)).to be_truthy
211+
expect(@model.has_attribute?(:column_b)).to be_truthy
212+
expect(@model.has_attribute?(:column_c)).to be_falsey
213+
end
214+
215+
it "allows has_attribute? to be explicitly stubbed" do
216+
@model = mock_model(MockableModel, :has_attribute? => false)
217+
expect(@model.has_attribute?(:column_a)).to be_falsey
218+
expect(@model.has_attribute?(:column_b)).to be_falsey
219+
end
220+
end
221+
end
222+
196223
describe "#respond_to?" do
197224
context "with an ActiveRecord model" do
198225
before(:each) do

0 commit comments

Comments
 (0)