Giter Site home page Giter Site logo

mocknim's Introduction

mocknim

A tool for creating mocks. Current goal is that it should mock its own modules. So the package seems to be self-explanatory.

Mocknim framework

If you should choose to compose your modules like it is done here, you can count on this package to provide you with mocks needed. The requirements are as follows:

  • one type is declared in each module file with the name of the file (first letter is upper case)
  • one or many constructor procedures are present which return a single object of the type
  • other procedures must have the first parameter of type that is declared in the module
  • there may be other types and proceduders so long as they are not visible outside of the module

Example

Install Nim, install Nimble and

https://github.com/mem-memov/mocknim
cd mocknim
nimble test

Test files in the tests directory must be named with the lower letter t at the front. As the recursive directory traversal is missing in the current nimble test implementation one of possible naming strategies could be:

  • start with test_
  • add each subfolger as subfolder_
  • finish with module file name myModule.nim

The file name may look like this: test_myPackage_subfolder_myModule.nim

In test file the following sections are not unusual:

import unittest, mocknim

# generate all types and procedures needed to test this module
mock("MyPackage/subfolder/myModule")

suite "MyPackage/subfolder/myModule": # the suite name is arbitrary

  teardown:
    # reset mocks after each test and check number of procedure calls in the test
    unmock("MyPackage/subfolder/myModule")

  test "it can do something":

    # create mock objects that are used inside procedure under test
    let
      myDependency = mockMyDependency()

    # provide for execution flow of procedure under test

    myDependency.expects
      .doSomeThingWithTheDataAndReturn &= (("my data",), "my_result")

    # create real object

    let myModule = newMyModule(myDependency)

    # execute procedure under test

    let output = myModule.doSomeProcessing()

    # check the output with the means of "nim unittest"
    

Nim Unittest

State is stored in a structure of the type that is defined in the module file.

Procedurs

  • Constructor

It takes some or none arguments and returns one structure carrying the state.

var a = mockA()
a.expects.newA &= ((), a)
  • Action

It has at least one argument. Its first argument is always of the state type. This is to comply with Uniform Function Call Syntax (UFCS). Other arguments may or may not be passed to it. It may return one or no result. Types of the arguments and the result may be chosen at will. When you specify which procedure calls you expect to be executed wenn your test runs, you should omit the fist argument that keeps the state.

var a = mockA()
var b = mockB()
a.expects.doWithout &= ((), ())
a.expects.doWithOne &= ((arg,), ())
a.expects.doWithoutAndReturn &= ((), b)
a.expects.doWithTwoAndReturn &= ((arg_1, arg_2), b))

Limitations

Automatically are mocked only modules which can be loaded from actual files of the project.

mocknim's People

Contributors

mem-memov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.