如何在茉莉花和咖啡中编写开关盒的单元测试
How to write unit test for switch case in jasmine and coffee
我是jasmine
和coffee
的新手。
我的问题在别人看来很熟悉,但其实不然。
请看看我的代码片段
<<p> 咖啡代码/strong>$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)
如何在jasmine中开始编写测试用例?
您的代码片段的问题是,它生成一个随机的id
,因此它不能被确定性地测试。但是,您可以测试的是mainHead
和subHead
相对于id
是否发生了相应的变化。因此,我将该代码片段拆分为一个可测试的函数,其中包含switch
语句和其他语句:
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)
相关文章:
- 我的单元测试选项是什么
- 如何在Angular单元测试中从另一个控制器的rootScope将方法添加到rootScope中
- 使用量角器的当前url单元测试的getTitle
- AngularJS指令单元测试中未定义的函数
- 如何在ember单元测试中模拟_super()方法
- 单元测试依赖关系没有被嘲笑
- 为什么不'当单元测试出现解析错误时,我的因果报应测试会失败
- 在node.js中编写单元测试的最佳方式是什么
- Ng点击-如何进行行为单元测试
- Jasmine单元测试在监视服务方法时失败
- Angular 2和服务单元测试
- 在我的目录结构中,将单元测试助手源文件放在哪里
- 如何对CSS transitionEnd事件后执行的JavaScript进行单元测试
- 使用Node.js在应用程序引擎上进行本地单元测试
- 我应该如何衡量JS的单元测试覆盖率?我可以'我对jscoverage不满意
- 如何使用KARMA对React JSX ES6代码进行单元测试
- 如何在单元测试中将存储注入Ember.Service
- 单元测试:使用酶模拟父组件中子组件的点击事件
- Javascript单元测试
- 如何在茉莉花和咖啡中编写开关盒的单元测试