我使用AngularJS 1.2.16和Jasmine 2.X作为我的javascript规范 但他们很快变得凌乱。我很难找到有关如何重构和构建规范的信息。
以下是我的一些不良规格:
channel = mockRestangular = $httpBackend = deferred = undefined
channel_id = {...}
beforeEach ->
module("channels", ($provide) ->
mockRestangular = {
configuration: { baseUrl: "" }
one: ->
this
post: ->
this
put: ->
this
...
}
module ($provide) ->
$provide.value('Restangular', mockRestangular)
return
)
beforeEach inject((_channel_, $q, $injector) ->
channel = _channel_
$httpBackend = $injector.get('$httpBackend')
deferred = $q.defer()
)
it "spec1", inject(($q, $rootScope) ->
deferred = $q.defer()
spyOn(mockRestangular.one().one(), 'get').and.returnValue(deferred.promise)
spyOn(channel::, 'init').and.stub()
new_channel = new channel(channel_id)
new_channel.updateCount()
deferred.resolve({"channels":[{...long...long...object...}]})
$rootScope.$digest()
expect(new_channel.meta.totalProducts).toEqual(24849)
expect(new_channel.meta.activeProducts).toEqual(1349)
)
it "spec2", inject(($q, $rootScope) ->
deferred = $q.defer()
spyOn(mockRestangular.one().one(), 'get').and.returnValue(deferred.promise)
spyOn(channel::, 'init').and.stub()
new_channel = new channel(channel_id)
new_channel.updateStatisticsRevenue()
deferred.resolve({"revenue_statistics":[{...another...very...very...long...object...}]})
$rootScope.$digest()
expect(new_channel.statistics.revenue).toEqual([{...kinda...long...object...result...}])
)
# spec with real respond-mock objects
describe "describtor2", ->
it "spec3", inject(($rootScope) ->
$httpBackend.expectPUT().respond(201,
{products:[{"sku":"10413161","active":false,"min_price":{"fractional":400,"currency":"EUR"},"max_price":{"fractional":950,"currency":"EUR"}},{"sku":"10413162","active":true,"min_price":{"fractional":458,"currency":"EUR"},"max_price":{"fractional":799,"currency":"EUR"}}]})
spyOn(mockRestangular.one().one(), 'get').and.returnValue(deferred.promise)
spyOn(channel::, 'init').and.stub()
new_channel = new channel channel_id
new_channel.updateProducts()
new_channel.getMeta().activeProducts = 2
expect(mockRestangular.one().one().get).toHaveBeenCalled
deferred.resolve({"products":[{"sku":"10413161","active":true,"min_price":{"fractional":412,"currency":"EUR"},"max_price":{"fractional":890,"currency":"EUR"}},{"sku":"10413162","active":true,"min_price":{"fractional":448,"currency":"EUR"},"max_price":{"fractional":799,"currency":"EUR"}}]}
)
$rootScope.$digest()
new_channel.updateProduct([{sku:"10413161",active:false,min_price:{fractional:400,currency:"EUR"},max_price:{fractional:950,currency:"EUR"}},{"sku":"10413162","active":true,"min_price":{"fractional":458,"currency":"EUR"},"max_price":{"fractional":799,"currency":"EUR"}}])
$httpBackend.flush()
expect(new_channel.getProducts()).toEqual(
[{"sku":"10413161","active":false,"min_price":{"fractional":400,"currency":"EUR"},"max_price":{"fractional":950,"currency":"EUR"}},{"sku":"10413162","active":true,"min_price":{"fractional":458,"currency":"EUR"},"max_price":{"fractional":799,"currency":"EUR"}}]
)
expect(new_channel.getMeta().activeProducts).toBe(1)
)
因为它们中包含所有物体的时间太长,所以我甚至开始投入更多的物品,并且期待"单一规格。我知道这是错的,但我害怕那些巨大的规格。
是否有任何构建或重构Jasmine规范的最佳实践?
答案 0 :(得分:1)
使用BeforeEach
放置每个规范的一些初始公共代码,例如,您可以放置这些行:
deferred = $q.defer()
spyOn(mockRestangular.one().one(), 'get').and.returnValue(deferred.promise)
spyOn(channel::, 'init').and.stub()
new_channel = new channel(channel_id)
与BeforeEach
关联的describe
中的。
beforeEach(function() {
deferred = $q.defer();
spyOn(mockRestangular.one().one(), 'get').and.returnValue(deferred.promise);
spyOn(channel::, 'init').and.stub();
new_channel = new channel(channel_id);
});
其他选择:创建一些基本的javascript函数来收集公共代码。 优点是您可以命名代码的这些部分:
function mockDBGet() {
deferred = $q.defer();
spyOn(mockRestangular.one().one(), 'get').and.returnValue(deferred.promise);
}
function initChannel() {
spyOn(channel::, 'init').and.stub();
new_channel = new channel(channel_id);
}
//.......
it('myCurrentSpec', function(){
mockDBGet();
initChannel(); far more clean than your previous version
});