Giter VIP home page Giter VIP logo

luamock's Introduction

Testing Framework for Lua - LuaMock Build Status

The one lua testing framework that mocks classes, runs with real classes from your project, has nginx embeded methods for openresty individual testing. Has a suite of libraries preinstalled and you can specify libraries to install.

LuaMock runs better in docker than in standalone - all you need to do is pull the image and run it like so

Table of contents

  1. Installing
  2. Usage inside test classes
    1. Mocking
    2. Running and skipping tests
    3. Assertions
  3. Dependencies
  4. Contributing

Installing

  • sudo luarocks make mocka-1.0-1.rockspec
  • sudo luarocks install luacov

Optional:

  • sudo luarocks install luacov-coveralls
  • lua -lluacov run_tests.lua
  • luacov
  • luacov-cobertura -o coverage_report.xml

Sugest using:

GitHub Pull Request Coverage Status and Cobertura Plugin

Usage inside test classes

spy(...) - spying and stubbing

Spy whatever classes you want. Spies are reset after each test. And loose whatever stubbing you did. That is why if any stub is needed than it should be declared at beforeEach level. T2 is a global required field in this example - required somewhere else.

Stubs treat even the async nature of nginx - ran on demand

    beforeEach(function()
        spy("test2", "run", function()
            print "Ok"
        end)
    end)
    test('it is', function()
        t2:run()
        calls(spy("test2").run, 1)
    end)

mock(...) - Mocking

Mock whatever methods you want

    local classToMock = mock("path.to.class", {"method1", "method2"})

or mock all the methods dynamic

    local classToMock = mock("path.to.class")

Alter mock behaviour

    local classToMock = mock("path.to.class", {"method1", "method2"})
    when(classToMock).method1.fake(function(arg1, arg2)
        return arg1
    end)

or

    local classToMock = mock("path.to.class", {"method1", "method2"})
    when(classToMock).method1.doReturn = function(arg1, arg2)
            return arg1
    end

or

    local classToMock = mock("path.to.class", {"method1", "method2"})
    classToMock.__method1.doReturn = function(arg1, arg2)
        return arg1
    end

The only method that you don't want to alter if you mock is the new function.

test(...) and xtest(...) - Running and Skipping

Running a test

    test('describe what you are testing', function()
        assertEquals(true, true)
    end)

Running a test that throws exception or you know it will fail

    test('describe what you are testing', function()
        assertEquals(true, false)
    end, true)

Skipping a test

    xtest('describe what you are testing', function()
        assertEquals(true, true)
    end)

Assertions

assertEquals

    assertEquals(true, true)
    local one_object = {
        one = 1,
        two = 2
    }
    local second_object = {
        two = 2,
        one = 1
    }
    
    assertEquals(one_object, second_object)
    assertEqualse("string", "string")
    assertEquals(1,1)
    local first_array = {1, 2, 3}
    local second_array = {1, 2, 3}
    assertEquals(first_array, second_array)

assertNotEquals

    local first_array = {1, 2, 3}
    local second_array = {2, 1, 3}
    assertNotEquals(first_array, second_array)

assertNil

    assertNil(x)

assertNotNil

    local x = "string"
    assertNotNil(x)

calls(...) - verify a mock has been called

    local classToMock = mock("class", {"method"})
    calls(classToMock.__method, 1)
    local classToMock = mock("class", {"method"})
    calls(classToMock.__method, 1, arg2, arg1)

Dependencies

This dependencies apply for docker

luamock's People

Contributors

atrifan avatar corinapurcarea avatar vdatcu avatar

Watchers

Fil Maj avatar James Cloos 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.