次のCoffeescriptコードを使用して、1つのbackbone.jsビューの初期化が別のビューを構築することを検証しています。
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()
これはうまく機能しますが、私はそれを改善することが可能であるはずだと思わずにはいられません。私は次のような構文を想像しています:
it 'creates a new MatricesView ', ->
expect(Avia.MatricesView).toHaveBeenCalledMatching((args...) => args[0].attr('id') == 'tabs-3')
... wheretoHaveBeenCalledMatching
は、引数のスプラットを取り、成功を示すためにtrueを返し、それ以外の場合はfalseを返す関数を取ります。
誰かがこのようなものに出くわしたことがありますか、それとも私はここで自分自身を転がす必要がありますか?または、このコードを改善する方法についてのより良い提案はありますか?