Coder Social home page Coder Social logo

testspy's Introduction

TestSpy

Swift 5.0 Build Status Carthage compatible Pod version codecov

Swift Framework for Spy Objects

Requirements

  • Swift: 4.0+
  • iOS: 9+

Create a spy object

To create a spy object you just have to implement the TestSpy protocol on your test class.

class TestClass: TestSpy {
     enum Method: Equatable {
         case test
         case testWithArgument(arument: Int)
     }
     
     var callstack = CallstackContainer<Method>()
}

Then when the method you want to test is called you have to record the method in the callstack

extension TestClass: TestProtocol {
    func test() {
        callstack.record(.test)
    }
    
    func testWithArgument(argument: Int) {
        callstack.record(.testWithArgument(argument: argument))
    }
}

Use the spy object in tests

XCTAssertTrue(spyObject.check(method: .test, predicate: CallstackMatcher.any))

Use the spy object in tests with Nimble

expect(spyObject).to(haveReceived(.test))

Callstack Matchers

There are some default matchers that can be used on the test to check that callstack content.

The main matchers are:

  • times(Int)
  • atLeast(times: Int)
  • never
  • any
  • before(Method)
  • immediatelyBefore(Method)
  • after(Method)
  • immediatelyAfter(Method)

Usage

XCTAssertTrue(spyObject.check(method: .test, predicate: CallstackMatcher.before(.testWithArgument(argument: 1))))

Usage with Nimble

expect(spyObject).to(haveReceived(.test), .before(.testWithArgument(argument: 1)))

Automatically generate Spy Objects with Sourcery

Sourcery offers a good way to automatically generate spy objects. You can find an example stancil file to generate Spy Objects here

to use it:

  • add a script phase, before the compile sources phase, in your test project with:
sourcery --sources "$SOURCESPATH" --templates "$TEMPLATESPATH" --output "$OUTPUTPATH" --args module="$CURRENTFRAMEWORKNAME",import="Foundation",import="UIKit"...
  • add this annotation to the protocol you want to spy // sourcery: autoSpy
  • include the files generated at $OUTPUTPATH on your test project

Author

Franco Meloni, [email protected]

License

TestSpy is available under the MIT license. See the LICENSE file for more info.

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.