0

I'm adding Jasmine to a large project in order to add tests to that project's javascript. Normally I use Ruby and I'm a little out of my element here.

I have a class, that has a function and I want to create a spy for it so that it returns a certain value during one of my tests. Here's a summary of the code:

class @MyKlass
  current_location = ->
    window.location.host

  verify_domain: () ->
    domain_filter = current_location()
    domain_list = /example\.com/i
    @valid_domain = domain_filter.match(domain_list)?

So how would I do something like this?

  it("verifies domains", function() {
    spyOn(MyKlass, 'current_location').and.returnValue("example");
    var myKlass = new MyKlass();
    expect(myKlass.verify_domain()).toEqual(true);
  });
RobertH
  • 528
  • 3
  • 13

1 Answers1

0

So it turns out that this was a function on an instance of the class- which I had just missed somehow in the code. Here's the abbreviated solution:

describe('MyKlass', function() {
  myKlass = null

  beforeEach(function() {
    myKlass = new MyKlass()
  });

  it('returns true for our domains', function() {
    spyOn(myKlass, 'verify_domain').and.returnValue(true);
    expect(myKlass.verify_domain()).toBe(true);
  }); 
RobertH
  • 528
  • 3
  • 13