Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing the viewmodels and mocking the dependencies

I'm pretty new in the javascript testing world and I'm having problems implementing some in my hottowel application. Most of the examples that I found online don't go as far as testing amd/require and the ones about amd/require don't show some other stuff.

I'm trying to test my vm by passing a mock service, let's say...

viewModel:

define(['services/dataService'], function (dataService) { function activate() { dataService.returnSomething(); } });

Can someone point me in the right direction (ideally a concrete example) on how to achieve this? Any test framework and mock library is ok.

Thanks

like image 326
Lucas Avatar asked Aug 19 '13 04:08

Lucas


People also ask

What are stubs and mocks in unit testing?

Both mocks and stubs are test doubles, which are code constructs used during software testing to stand in for actual objects and services. Mocks verify the behavior of the code you're testing, also known as the system under test. Mocks should be used when you want to test the order in which functions are called.

What does mocking mean in unit testing?

A quick overview of mocking Mocking is a way to replace a dependency in a unit under test with a stand-in for that dependency. The stand-in allows the unit under test to be tested without invoking the real dependency.

How do you write a unit test case for a Viewmodel?

In this codelab, you use the JUnit framework to write unit tests. To use the framework, you need to add it as a dependency in your app module's build. gradle file. You can now use this library in your app's source code, and Android studio will help to add it to the generated Application Package File (APK) file.

What is mocking in unit testing Swift?

In automated tests, it is creating an object that conforms to the same behavior as the real object it is mocking. Many times the object you want to test has a dependency on an object that you have no control over. There are several ways to create iOS unit testing mock objects. One way is to subclass it.


1 Answers

I'm currently using jasmine to unit test my viewmodels.

With Jasmine you have an HTML page that executes all your ViewModels. It allows you to mock out functions. The page I linked to, contains a complete description of what you can do with Jasmine.

Example:

var dataService = Require("services/dataService");
spyOn(dataService , 'returnSomething').andReturn("something");
// execute the system under test here
expect(dataService.returnSomething).toHaveBeenCalled();
like image 194
Kenneth Avatar answered Sep 28 '22 02:09

Kenneth