1

I have a controller which has some logic that runs when the controller loads.

This logic checks a value in a service and performs an action depending on the value.

if(FunkyService.foo()) {
    doA();
} else {
    doB();
}

I want to write a Jasmine test that can test outcomes for each value that the service can have (In this example true or false in function foo() of my service).

My problem is that the controller is loaded in a beforeEach() function which means that it is too late in my tests to switch the service value. If I set the results of my services foo() function in a beforeEach() I can't understand how to test both true and false outcomes.

I have two questions.

  • How can I get my controller to reload in one of my tests? This would allow me to change the service foo() function return value and then force the controller to re-run the logic when it loads.

  • Or should I be approaching this in an entirely different way?

1 Answer 1

2

With all the unit tests I've done any activation or controller level changes I put into separate describes for in this example I'm assuming doA and doB sets a variable to true / false (what is passed into the service).

describe("Setting up globals", function() {
  var foo;

  beforeEach(function() {
    foo = false;
  });

  it("is just a function, so it can contain any code", function() {
    expect(foo).toBe(false);
  });

  it("can have more than one expectation", function() {
    expect(true).toEqual(true);
  });

  describe("testing service for true", function() {
    var sut;

    beforeEach(function() {
      sut = new FunkyService(true);
    });

    it("can reference both scopes as needed", function() {
      expect(foo).toEqual(true);
    });
  });



  describe("testing service for false default action", function() {
    var sut;

    beforeEach(function() {
      sut = new FunkyService(false);
    });

    it("can reference both scopes as needed", function() {
      expect(foo).toEqual(false);
    });
  });

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

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.