我是jasmine
和coffee
的新手。
我的问题对其他人来说很熟悉,但事实并非如此。
请查看我的代码段
咖啡代码
$scope.changeText = ->
$timeout $scope.changeText, 5000
id = parseInt(Math.random() * 4)
switch id
when 0
$scope.home.banner.mainHead = 'Some Heading'
$scope.home.banner.subHead = 'Some more text'
when 1
$scope.home.banner.mainHead = 'Some other Heading'
$scope.home.banner.subHead = 'Some more text'
when 2
$scope.home.banner.mainHead = 'Another Heading'
$scope.home.banner.subHead = 'Another Text'
when 3
$scope.home.banner.mainHead = 'Another Heading 1'
$scope.home.banner.subHead = 'Text text text'
return
$timeout($scope.changeText, 5000)
如何开始在茉莉花中编写测试用例?
提前谢谢。
答案 0 :(得分:0)
您的代码段问题在于它会生成随机id
,因此无法确定性地进行测试。但是,您可以测试的是mainHead
和subHead
是否相应地更改了id
。因此,我会将该代码段拆分为带有switch
语句和其余语句的可测试函数:
COFFEE:
getBanner = (id) ->
banner = {}
switch id
when 0
banner.mainHead = "Some Heading"
banner.subHead = "Some more text"
when 1
banner.mainHead = "Some other Heading"
banner.subHead = "Some more text"
when 2
banner.mainHead = "Another Heading"
banner.subHead = "Another text"
when 3
banner.mainHead = "Another Heading 1"
banner.subHead = "Text text text"
banner
$scope.changeText = ->
id = parseInt(Math.random() * 4)
$scope.home.banner = getBanner(id)
# $interval might be better in your use case than $timeout
$interval $scope.changeText, 5000
测试:
describe "getBanner", ->
it "should return mainHead/subHead", ->
banner = getBanner(0)
expect(banner.mainHead).toEqual("Some Heading")
expect(banner.subHead).toEqual("Some more text")
banner = getBanner(1)
# ...
it "should return empty banner on wrong id", ->
# testing edge cases
banner = getBanner(-1)
expect(Object.keys(banner).length).toEqual(0)