10

I have an object that I am mocking up for unit testing. Essentially in my test file I mock it up as follows:

var mockObject = {
    mockMethod1 : function() {return true},
    mockMethod2 : function() {return true}
};


beforeEach(module('myModule') , function ($provide) {
    $provide.value('realObject',mockObject);
});

The way i understand it is that as I test functionality in my module etc... anywhere that references the "realObject" will use my "mockObject"

My issue is that I have made multiple js files for testing and I do not want to define my "mockObject" in each one of them ... nor do i want to maintain it in any more places than i have too.

Is there a way to move my "mockObjact" to a seperate file that gets included in karma.conf.js that will make the "mockObject" available for injection into any of my test files ..... Im thinking along the lines of how you inject $rootScope

Deslyxia
  • 619
  • 4
  • 11
  • 32

3 Answers3

5

You can create a global beforeEach function if it is written outside the context of a specific suite, but still executed by Jasmine, e.g. create a custom file to load by Karma and write your beforeEach function there without enclosing it in a describe function.

Example:

var myGlobal;

beforeEach(function() {
    // This will run before any it function.
    // Resetting a global state so the change in this function is testable
   myGlobal = 10
});

describe('first suite', function(){
  it('is a test', function(){
      expect(myGlobal).toBe(10);
      // Set the value to show that beforeEach is executed for each it function
      myGlobal = 20;
      expect(myGlobal).toBe(20);
  });

  it('is another test', function(){
      expect(myGlobal).toBe(10);
      myGlobal = 30;
      expect(myGlobal).toBe(30);
  });
});

describe('second suite', function(){
  it('is a test', function(){
      expect(myGlobal).toBe(10);
  });
});

See fiddle here

Eitan Peer
  • 4,335
  • 27
  • 29
  • 1
    Yeah i currently have it written as a global in a test file ... the problem is that I want to use this mock object in multiple test files and want to maintain as few copies of this object as possible . – Deslyxia Apr 05 '14 at 09:28
  • What is the global? the mock object? Are you $providing the service value in multiple files? Since that's what the answer is about - providing the mocked value in a single file for all tests – Eitan Peer Apr 06 '14 at 06:56
  • I guess what im unclear on is exactly how to use $provide to get to the function in the newly created service that will return the mockObject. what i was doing before was just creating the mock object in the test and then using provide. – Deslyxia Apr 06 '14 at 07:59
  • That's what you should be doing. The thing is that you can do it only once, in a single file in a single beforeEach functon which will run before any test if that beforeEach is not part of a suite – Eitan Peer Apr 06 '14 at 08:37
  • 1
    there is still a piece to this missing ... In my test file i create an mockObject ... I use provide to get the mockObject to be used in my tests instead of the "realObject" .... all of that works fine ... the missing piece is that the definition of what my mock object "is" now resides in a service so i can use it in multiple tests files and only manage it in one spot. How exactly do i do something along the lines of var mockObject = myService.getMockObject() – Deslyxia Apr 07 '14 at 16:30
  • You can create the mockObject on the global scope, then it is accessible in all tests. You can preferably create a "namepsace" on the global scope with your global mock objects, e.g. window.mocks = {myMock: jasmine.createSpy('spy name here')}; – Eitan Peer Apr 08 '14 at 08:07
3

You could build a service that houses your mock, and inject that service in each test file.

beforeEach(inject(function($rootScope, $controller, yourMockSvc) {
    appScope = $rootScope.$new();
    appCtrl = $controller('AppController', {
        $scope: appScope,
        yourSvc: yourMockSvc
    });
}));
aet
  • 7,192
  • 3
  • 27
  • 25
  • i hear where you are going with making it a service. But the way i would envision it is being something where I would want to be able to setup something at the top of my test like: var myMockObject = myService.getObject(); and then use that mock object in the rest of the tests – Deslyxia Apr 05 '14 at 07:38
1

Just define your mock object inside of another file and export it. You should be able to use it in any file.

Archmede
  • 1,592
  • 2
  • 20
  • 37