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

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

6 Comments

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 .
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
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.
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
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()
|
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
    });
}));

1 Comment

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
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.