Skip to content

Instantly share code, notes, and snippets.

Created September 2, 2010 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/562203 to your computer and use it in GitHub Desktop.
Save anonymous/562203 to your computer and use it in GitHub Desktop.
describe HasAndBelongsToManyActiveResource do
class Resource
end
class Record
include HasAndBelongsToManyActiveResource
end
before(:each) do
@resource = Resource.new
@resource.stub(:id).and_return('1')
Resource.should_receive(:find).and_return([@resource]) # fails here
Resource.should_receive(:new).and_return(@resource)
@record = Record.new
@record.stub(:id).and_return('2')
@record.stub(:resource_ids).and_return([1])
@record.stub(:current_language).and_return('de')
Record.should_receive(:serialize).and_return(true)
Record.should_receive(:new).and_return(@record)
end
if "should ..."
@record.class.has_and_belongs_to_many_active_resource :resourcen
end
end
Spec::Mocks::MockExpectationError in 'HasAndBelongsToManyActiveResource should ...'
<Resource (class)> expected :find with (any args) once, but received it 0 times
./spec/models/module_spec.rb:17:
module HasAndBelongsToManyActiveResource
def self.included(base)
def base.has_and_belongs_to_many_active_resource resource, options={}
resource = resource.to_s.singularize
attribute = options[:attribute] || resource.foreign_key.pluralize
klass = (options[:class_name] || resource.classify).constantize
self.serialize attribute
define_method resource.pluralize do |person_id|
values = self.send attribute
return [] if values.blank?
klass.find :all, :params => { :ids => values, :person_id => person_id, :language => current_language }
end
end
end
describe HasAndBelongsToManyActiveResource do
class Resource
end
class Record
include HasAndBelongsToManyActiveResource
end
before(:each) do
@resource = Resource.new
@resource.stub(:id).and_return('1')
Resource.stub(:find).and_return([@resource]) # fails here
Resource.stub(:new).and_return(@resource)
@record = Record.new
@record.stub(:id).and_return('2')
@record.stub(:resource_ids).and_return([1])
@record.stub(:current_language).and_return('de')
Record.stub(:serialize).and_return(true)
Record.stub(:new).and_return(@record)
end
if "should ..."
@record.class.has_and_belongs_to_many_active_resource :resourcen
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment