I'm using the following Coffeescript code to validate that initialization of one backbone.js view constructs another:
describe 'Avia.AviaView', -> beforeEach -> @aviaView = new Avia.AviaView(addFixtureDiv('avia')) @matricesView = new Backbone.View() spyOn(Avia, 'MatricesView').andCallFake( (element) => if !element throw "Expected MatricesView to be constructed with a parent element" else if element.attr('id') != 'tabs-3' throw "Expected MatricesView to be constructed with the parent element #tabs-3" else @matricesView ) describe 'initialize', -> beforeEach -> @aviaView.initialize() it 'creates a new MatricesView ', -> expect(Avia.MatricesView).toHaveBeenCalledOnce()
This works nicely, but I can't help but think it ought to be possible to improve it. I'm imagining a syntax like:
it 'creates a new MatricesView ', -> expect(Avia.MatricesView).toHaveBeenCalledMatching((args...) => args.attr('id') == 'tabs-3')
toHaveBeenCalledMatching takes a function that takes a splat of the arguments, and returns truthy to indicate success, and falsy otherwise.
Has anyone come across something like this, or do I need to roll my own here? Or, has anyone a better suggestion of how to improve this code?