Click here to Skip to main content
11,483,983 members (66,033 online)
Click here to Skip to main content

MoqJS

, 8 Apr 2015 CPOL 3K 3
Rate this:
Please Sign up or sign in to vote.
Mocking library for JavaScript

Introduction

This is a simple framework for testing JavaScript/typescript based on the .NET Moq framework.

Background

I was a .NET programmer and was used to the Moq framework to do tests. When I switched to JavaScript, I had many options for mocking frameworks, but I was used to Moq and was surprised that there was no Moq implementation for JavaScript. So I decided to raise the glove and create one.

Using the Code

The framework does not work exactly as Moq used to because of the lack of interfaces in run time.

So the next best thing is to change the behaviour of existing objects.

This is why I decided the term mock is not appropriate and switched to the term mole which is more suitable here.

Some basic examples are given below:

// Create some object instance
var dog = new Dog();

// Create a mole for the object
var mole = new Mole(dog);

// Setup behavior
mole.setup(_dog => _dog.eat('meat')).returns('Yum yum yum');

// Invoke
var result = dog.eat('meat');

// Verify the given method was indeed called with the expected value exactly once
var isVerified = mole.verify(_dog => _dog.eat('meat'), Times.exact(1));

GitHub

You can find more examples, the build and the code in my git:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

slavik57

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pin
C R9-Apr-15 5:46
memberC R9-Apr-15 5:46 
GeneralRe: My vote of 5 Pin
slavik579-Apr-15 7:45
memberslavik579-Apr-15 7:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 8 Apr 2015
Article Copyright 2015 by slavik57
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid