{"id":"vocksel/mock","name":"mock","scope":"vocksel","platform":"roblox","description":"Mirrored from the Wally registry.","version":"0.1.0","latest":"0.1.0","versions":["0.1.0"],"license":"MIT","licenseRating":"safe","licenseCaveats":[],"licenseVerified":true,"dependencies":{},"integrity":"9e773d025b4224cf53f75939d95d00f0372dbd46eafc6ded2cf5d7f59c7c8a06","likes":0,"downloads":0,"install":"forest install vocksel/mock","url":"https://forest.dev/p/roblox/vocksel/mock","files":"https://api.forest.dev/ai/package/roblox/vocksel/mock/files","readme":"# Mock\n\n[![CI](https://github.com/vocksel/mock/actions/workflows/ci.yml/badge.svg)](https://github.com/vocksel/mock/actions/workflows/ci.yml)\n\nPackage for creating [Jest](https://jestjs.io/)-like mocks for use with unit testing.\n\n## Usage\n\n```lua\n-- example/init.lua\nlocal Players = game:GetService(\"Players\")\n\nlocal example = {}\n\n-- Exposing the Players service allows it to be mocked in tests\nexample.Players = Players\n\nfunction example:getCharacters()\n\tlocal characters = {}\n\tfor _, player in ipairs(example.Players:GetPlayers()) do\n\t\tif player.Character then\n\t\t\ttable.insert(characters, player.Character)\n\t\tend\n\tend\n\treturn characters\nend\n\nreturn example\n```\n\n```lua\n-- example/init.spec.lua\nreturn function()\n\tlocal Mock = require(game.ReplicatedStorage.DevPackages.Mock)\n\tlocal example = require(script.Parent)\n\n\tit(\"should get all the characters in the experience\", function()\n\t\tlocal mockPlayers = Mock.new()\n\n\t\t-- Create a mock to represent a Player\n\t\tlocal mockPlayer = Mock.new()\n\t\tmockPlayer.Character = Instance.new(\"Model\")\n\n\t\t-- Define the implementation of Players:GetPlayers()\n\t\tmockPlayers.GetPlayers:mockImplementation(function()\n\t\t\treturn {\n\t\t\t\tmockPlayer,\n\t\t\t}\n\t\tend)\n\n\t\t-- Stub out the actual Players service with a mock\n\t\texample.Players = mockPlayers\n\n\t\t-- Our mocks are setup, so now we can test the getCharacters() function\n\t\tlocal characters = example:getCharacters()\n\n\t\texpect(#characters).to.equal(1)\n\t\texpect(characters[1]).to.equal(mockPlayer.Character)\n\tend)\nend\n```\n\n## Installation\n\n### Wally\n\nIf you are using [Wally](https://github.com/UpliftGames/wally), add the following to your `wally.toml` and run `wally install` to get a copy of the package.\n\n```\n[dev-dependencies]\nMock = \"vocksel/mock@v0.1.0\n```\n\n### Roblox Studio\n\n* Download a copy of the rbxm from the [releases page](https://github.com/vocksel/mock/releases/latest) under the Assets section. \n* Drag and drop the file into Roblox Studio to add it to your experience.\n\n## API\n\n**`Mock.new(): Mock`**\n\nReturns a new Mock instance.\n\nUsage:\n\n```lua\nlocal Mock = require(game.ReplicatedStorage.DevPackages.Mock)\n\nlocal mock = Mock.new()\n```\n\nWhen a mock is indexed, if the member does not exist (i.e. it is not listed in this API), a new Mock instance will be created. This allows you to build complex structures for your mocks:\n\n```lua\nlocal mockPlayers = Mock.new()\n\n-- GetPlayers implicitly becomes a Mock \nmockPlayers.GetPlayers:mockImplementation(function()\n    return { \"Player1\", \"Player2\" }\nend)\n\nprint(mockPlayers:GetPlayers()) -- { \"Player1\", \"Player2\" }\nprint(#mockPlayers.GetPlayers.mock.calls) -- 1\n```\n\n**`Mock.is(other: any): boolean`**\n\nReturns true if `other` is a Mock instance. False otherwise.\n\nUsage:\n\n```lua\nlocal Mock = require(game.ReplicatedStorage.DevPackages.Mock)\n\nlocal mock = Mock.new()\n\nprint(Mock.is(mock)) -- true\nprint(Mock.is(\"mock\")) -- false\n```\n\n**`Mock.mock: table`**\n\nEach Mock instance comes with a `mock` object with the following fields:\n- `name: string`\n    - The name of the Mock. By default, this is set to `\"Mock\"`. When indexing a mock, the implicitly created mocks are named after the key that was indexed.\n        ```lua\n        local mock = Mock.new()\n        print(mock.foo.bar.mock.name) -- \"bar\"    \n        ```\n- `calls: table`\n    - An array containing arrays of the arguments passed each time the mock is called.\n    - It is helpful to get the length of this array to know how many times the mock was called.\n\n**`Mock:mockImplementation(callback: (...any) -> any): nil`**\n\nSets the callback that gets run when a mock is called.\n\nUsage:\n\n```lua\nlocal mock = Mock.new()\n\nmock.timesTwo:mockImplementation(function(x: number)\n    return x * 2\nend)\n\nprint(mock.timesTwo(10)) -- 20\n```\n\n**`Mock:reset(): nil`**\n\nResets the mock between test cases.\n\nUsage:\n\n```lua\nlocal mockFunction = Mock.new()\n\nmockFunction()\nprint(#mockFunction.mock.calls) -- 1\n\nmockFunction:reset()\nprint(#mockFunction.mock.calls) -- 0\n```\n\nIf you define your mocks in the global scope of your tests, you should call `reset` in the `afterEach` hook. Note that this method also clears the implementation, so to retain it between tests you should set the implementation in `beforeEach`.\n\n```lua\nlocal mock = Mock.new()\n\nbeforeEach(function()\n    mock:mockImplementation(function()\n        return \"implementation\"\n    end)\nend)\n\nafterEach(function()\n    mock:reset()\nend)\n```\n\n## Contributing\n\nSee the [contributing guide](CONTRIBUTING.md).\n\n## License\n\n[MIT License](LICENSE)","readmeTruncated":false}