File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed
lib/rspec/active_model/mocks
spec/rspec/active_model/mocks Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments