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
  • 11,230
  • 13
  • 64
  • 94

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,333
  • 1
  • 22
  • 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,377
  • 2
  • 25
  • 23
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
  • 36,125
  • 23
  • 149
  • 210