1
0
Fork 0

Foxx Repository: byExample

This commit is contained in:
Lucas Dohmen 2013-09-16 16:23:07 +02:00
parent cdf9415650
commit 7d432a8911
2 changed files with 27 additions and 2 deletions

View File

@ -94,6 +94,7 @@ function RepositoryMethodsSpec() {
modelData,
data,
example,
cursor,
id,
id_and_rev;
@ -103,6 +104,7 @@ function RepositoryMethodsSpec() {
id_and_rev = stub();
modelData = stub();
example = stub();
cursor = stub();
model = stub();
data = stub();
collection = stub();
@ -165,6 +167,26 @@ function RepositoryMethodsSpec() {
assertTrue(model instanceof ModelPrototype);
ModelPrototype.assertIsSatisfied();
collection.assertIsSatisfied();
},
testByExample: function () {
expect(collection)
.toReceive("byExample")
.withArguments(example)
.andReturn(cursor);
allow(cursor)
.toReceive("toArray")
.andReturn([data]);
ModelPrototype = mockConstructor(data);
instance = new FoxxRepository(collection, { model: ModelPrototype });
var models = instance.byExample(example);
assertTrue(models[0] instanceof ModelPrototype);
ModelPrototype.assertIsSatisfied();
collection.assertIsSatisfied();
}
};
}

View File

@ -196,9 +196,12 @@ _.extend(Repository.prototype, {
///
/// See the documentation of collection.
////////////////////////////////////////////////////////////////////////////////
byExample: function () {
byExample: function (example) {
'use strict';
this.collection.byExample.apply(this.collection, arguments);
var rawDocuments = this.collection.byExample(example).toArray();
return _.map(rawDocuments, function (rawDocument) {
return (new this.modelPrototype(rawDocument));
}, this);
},
////////////////////////////////////////////////////////////////////////////////