3

I got a custom Backbone.Collection class in Coffeescript.

I named it (it is responsible for pagination):

class SI.PaginatedCollection extends Backbone.Collection

I want to write Jasmine spec which will test do I extends that particular class.

Sorry for my English, I now it is probably horrible. ;)

PS I can parse Javascript, but Coffeescript would be ideal.

nothing-special-here
  • 9,754
  • 11
  • 60
  • 93

3 Answers3

9

It seems like overkill to me to test this, but you could do something like this:

describe "SI.PaginatedCollection", ->

  beforeEach ->
    @collection = new SI.PaginatedCollection()

  it "is a subclass of Backbone.Collection", ->
    expect(@collection instanceof Backbone.Collection).toBeTruthy()

If you’re going to be checking instanceof a lot and/or you care about descriptive Jasmine output, it would be worth making a custom matcher so you could write this:

expect(@collection).toBeInstanceOf(Backbone.Collection)
Buck Doyle
  • 6,284
  • 1
  • 21
  • 35
3

In Jasmine 2.0 you can use jasmine.any() matcher. E.g:

collection = new SI.PaginatedCollection();

expect(collection).toEqual(jasmine.any(Backbone.Collection));

as mentioned in this blogpost

average Joe
  • 4,029
  • 1
  • 23
  • 22
0

There is not proper way to get the super reference, nor in JavaScript neither in Backbone, even using the __super__ Backbone method is not advisable by the documentation.

I think the cleanest approach is manually brand your subclasses with a pseudo-static attribute like:

var SI.PaginatedCollection = Backbone.Collection.extend({
  parent: "Backbone.Collection"
});

Any time you need to check an instance is from an specific parent just check the myInstance.parent.

Community
  • 1
  • 1
fguillen
  • 32,852
  • 19
  • 126
  • 192