{"id":15371570,"url":"https://github.com/stevehobbsdev/mongo-mocks","last_synced_at":"2026-03-18T01:30:17.474Z","repository":{"id":143406430,"uuid":"62563198","full_name":"stevehobbsdev/mongo-mocks","owner":"stevehobbsdev","description":"A mocking helper library for Mockito + PlayReactiveMongo","archived":false,"fork":false,"pushed_at":"2016-10-27T23:05:18.000Z","size":29,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-31T20:16:00.972Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stevehobbsdev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-07-04T13:11:27.000Z","updated_at":"2016-07-05T09:39:01.000Z","dependencies_parsed_at":null,"dependency_job_id":"499e0d40-b089-43bb-bc46-1371a126a8bf","html_url":"https://github.com/stevehobbsdev/mongo-mocks","commit_stats":{"total_commits":28,"total_committers":2,"mean_commits":14.0,"dds":0.1071428571428571,"last_synced_commit":"73a884d88f78af6bfb4c4da6ab2421a313bc2635"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevehobbsdev%2Fmongo-mocks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevehobbsdev%2Fmongo-mocks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevehobbsdev%2Fmongo-mocks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevehobbsdev%2Fmongo-mocks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stevehobbsdev","download_url":"https://codeload.github.com/stevehobbsdev/mongo-mocks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239929530,"owners_count":19720151,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-01T13:47:42.626Z","updated_at":"2026-03-18T01:30:17.401Z","avatar_url":"https://github.com/stevehobbsdev.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongo-mocks\n\n[![Build Status](https://travis-ci.org/elkdanger/mongo-mocks.svg?branch=master)](https://travis-ci.org/elkdanger/mongo-mocks)\n[ ![Download](https://api.bintray.com/packages/elkdanger/maven/mongo-mocks/images/download.svg) ](https://bintray.com/elkdanger/maven/mongo-mocks/_latestVersion)\n\nA mocking helper library for Mockito + PlayReactiveMongo. It helps you create basic mocks for:\n\n* Find operations, using either a collection or a single object\n* Insert operations\n* Update operations\n\n## Installation\n\nAdd a new resolver to your `build.sbt` file:\n\n```scala\nresolvers += Resolver.url(\"Elkdanger on Bintray\",\n    url(\"https://dl.bintray.com/elkdanger/maven\"))\n    (Resolver.ivyStylePatterns)\n```\n\nThen add the package as a dependency:\n\n```scala\nlibraryDependencies += \"org.elkdanger\" %% \"mongo-mocks\" % \"0.0.8\"\n```\n\nTo make use of the methods described below, mix in the `MongoMocks` trait into your spec class:\n\n```scala\nimport org.elkdanger.testing.MongoMocks\n\nclass MySpec extends MongoMocks {\n\n}\n```\n\n## Overview\n\nSetups are performed on `JSONCollection` objects that have been mocked, either through `mock[JSONCollection]` or by using the helper `MockCollection()`.\n\nAll of the setup methods below can also be used either on `mock[JSONCollection]` or `MockCollectionBuilder` (as returned by `MockCollection()`). `MockCollectionBuilder` can also be implicitly converted to `JSONCollection` to make passing it around a lot easier.\n\n## Finds that return a single entity\n\n\u003e Note: The find* methods usually have a DSL sugar method, or a normal method name - both are equivalent\n\nThe collection can be set up to return a single item when `find` is used. This essentially mocks the `one[T]` method:\n\n```scala \nval collection = MockCollection()\nval thing = mock[TestObject]\n\ncollection ~\u003e Some(thing)\n\n// OR...\n\ncollection.setupFind(Some(thing))\n\n// collection.find(Json.obj()).one[TestObject] will \n// return a Future[Option[TestObject]] containing thing\n\n```\n\nMost of the time you will provide a filter to narrow down the thing you want to return. This is done using the ? method, before providing the return value:\n\n```scala\nval collection = MockCollection()\nval thing = mock[TestObject]\n\ncollection ? (\"id\" -\u003e 1) ~\u003e Some(thing)\n\n// OR...\n\ncollection.setupFind(Json.obj(\"id\" -\u003e 1), Some(thing))\n\n// collection.find(Json.obj(\"id\" -\u003e 1)).one[TestObject] will \n// return a Future[Option[TestObject]] containing thing\n```\n\n## Finds that return collections\n\nThe same syntax can be used to set up finds that return collections of entities:\n\n```scala \nval collection = MockCollection()\nval listOfThings = List(mock[TestObject], mock[TestObject])\n\ncollection ~\u003e listOfThings\n\nOR...\n\ncollection.setupFind(listOfThings)\n\n// collection.find(Json.obj()).cursor[TestObject]().collect[List]() will \n// return a Future[List(TestObject)] containing thing\n\n```\n\nThis essentially mocks the query builder, cursor and collect methods on a Mongo collection. Filters can also be used in the same way to narrow down the results:\n\n```scala \nval collection = MockCollection()\nval listOfThings = List(mock[TestObject], mock[TestObject])\n\ncollection ? (\"country\" -\u003e \"UK\") ~\u003e listOfThings\n\nOR...\n\ncollection.setupFind(Json.obj(\"country\" -\u003e \"UK\"), listOfThings)\n\n// collection.find(Json.obj()).cursor[TestObject]().collect[List]() will \n// return a Future[List(TestObject)] containing thing\n\n```\n\n## Setting up inserts\n\nWhen using insert, you will want to mock the operation so that it returns a true or false:\n\n```scala\nval collection = MockCollection()\n\ncollection.setupAnyInsert(fails = true)   // fails can be true or false\n```\n\nOr to narrow it down to inserting a specific document, you can use the `\u003c~` method:\n\n```scala\nval collection = MockCollection()\nval obj = mock[TestObject]\n\ncollection \u003c~ obj\n\nOR...\n\ncollection.setupInsertWith(obj)   // equivalent\n```\n\n## Verifying inserts\n\nTo verify that an insert was made with a particular object:\n\n```scala\ncollection verifyInsertWith obj     // with either pass or fail assertion\n```\n\nTo verify that an insert was made with any object:\n\n```scala\ncollection.verifyAnyInsert\n```\n\nTo verify that an insert was made, capturing the result for further verification:\n\n```scala\nval captor = ArgumentCaptor.forClass(classOf[TestObject])\n\ncollection verifyInsertWith captor\n\n// captor.getValue will contain the object that was inserted\n```\n\n## Setting up updates\n\nWhen using update operations, you'll want to return a value to indicate whether an update was successful:\n\n```scala\ncollection.setupAnyUpdate(fails = true)   // can specify true or false to indicate success or failure\n```\n\nTo set up using a selector and an object:\n\n```scala\nval selector = Json.obj(\"id\" -\u003e 2)\nval obj = Json.obj(\"name\" -\u003e \"John Doe\")\nval collection = MockCollection()\n\ncollection.setupUpdate(selector, obj)\n```\n\n## Verifying updates\n\nTo verify that any update was performed:\n\n```scala\ncollection.verifyAnyUpdate\n```\n\nTo verify that an update was made, optionally checking either the selector, the object, or both:\n\n```scala\ncollection.verifyUpdate(\n    selectorFunc = { _ should be Json.obj(\"id\" -\u003e 1) })\n    \ncollection.verifyUpdate(\n    objectFunc = { _ should be Json.obj(\"name\" -\u003e \"John Doe\") })\n    \n// or a combination of the two:\ncollection.verifyUpdate(\n    selectorFunc = { _ should be Json.obj(\"id\" -\u003e 1) },\n    objectFunc = { _ should be Json.obj(\"name\" -\u003e \"John Doe\") })\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstevehobbsdev%2Fmongo-mocks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstevehobbsdev%2Fmongo-mocks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstevehobbsdev%2Fmongo-mocks/lists"}