<tfoot id='IUuaw'></tfoot>

<legend id='IUuaw'><style id='IUuaw'><dir id='IUuaw'><q id='IUuaw'></q></dir></style></legend>
  • <i id='IUuaw'><tr id='IUuaw'><dt id='IUuaw'><q id='IUuaw'><span id='IUuaw'><b id='IUuaw'><form id='IUuaw'><ins id='IUuaw'></ins><ul id='IUuaw'></ul><sub id='IUuaw'></sub></form><legend id='IUuaw'></legend><bdo id='IUuaw'><pre id='IUuaw'><center id='IUuaw'></center></pre></bdo></b><th id='IUuaw'></th></span></q></dt></tr></i><div id='IUuaw'><tfoot id='IUuaw'></tfoot><dl id='IUuaw'><fieldset id='IUuaw'></fieldset></dl></div>

    <small id='IUuaw'></small><noframes id='IUuaw'>

      <bdo id='IUuaw'></bdo><ul id='IUuaw'></ul>

      1. Jasmine spyOn 多次返回

        Jasmine spyOn with multiple returns(Jasmine spyOn 多次返回)
            <tbody id='eyboN'></tbody>

                <small id='eyboN'></small><noframes id='eyboN'>

                <tfoot id='eyboN'></tfoot>
              1. <i id='eyboN'><tr id='eyboN'><dt id='eyboN'><q id='eyboN'><span id='eyboN'><b id='eyboN'><form id='eyboN'><ins id='eyboN'></ins><ul id='eyboN'></ul><sub id='eyboN'></sub></form><legend id='eyboN'></legend><bdo id='eyboN'><pre id='eyboN'><center id='eyboN'></center></pre></bdo></b><th id='eyboN'></th></span></q></dt></tr></i><div id='eyboN'><tfoot id='eyboN'></tfoot><dl id='eyboN'><fieldset id='eyboN'></fieldset></dl></div>
                <legend id='eyboN'><style id='eyboN'><dir id='eyboN'><q id='eyboN'></q></dir></style></legend>
                • <bdo id='eyboN'></bdo><ul id='eyboN'></ul>
                  本文介绍了Jasmine spyOn 多次返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想用 Jasmine 测试我的 Angular 应用程序.所以我创建了一些测试,其中大多数都可以正常工作.但是,我的一项功能要求用户填写提示.测试无法填充这个提示,所以我用 spyOn(window,'prompt').and.returnValue('test') 模拟了它们.这有效,但只有一次.

                  I would like to test my angular application with Jasmine. So I created some tests, most of them work fine. But, one of my functions require the user to fill in an prompt. Tests can not fill this prompt, so I mocked them with spyOn(window,'prompt').and.returnValue('test'). This works, but only once.

                  当我添加两个组件(提示所在的函数)时,我想 spyOn 第一个提示的结果为test",第二个提示的结果为test2".我尝试这样做:

                  When I add two of my components (the function in which the prompt is located), I want to spyOn the first prompt with result 'test', and the second prompt with 'test2'. I tried doing this as follows:

                  it 'should place the component as last object in the form', ->
                  
                        spyOn(window, 'prompt').and.returnValue('test')
                        builder.addFormObject 'default', {component: 'test'}
                  
                        spyOn(window, 'prompt').and.returnValue('test2')
                        builder.addFormObject 'default', {component: 'test2'}
                  
                        expect(builder.forms['default'][0].name).toEqual('test')
                  

                  但这会产生以下错误:错误:提示已被监视这是很合乎逻辑的,但我不知道用 spyOn 返回的另一种方式.

                  But this gives the following error: Error: prompt has already been spied upon This is quite logical, but I don't know another way of returning with a spyOn.

                  所以,我想要的是:在第一个 addFormObject 之前,我想监视返回test"的提示.第二个 addFormObject 我想用 return 'test2' 监视

                  So, what I want is this: Before the first addFormObject I want to spy on the prompt which returns 'test'. And the second addFormObject I want to spy with return 'test2'

                  推荐答案

                  使用 spyOn 你可以返回模拟值并像下面的代码一样动态设置它

                  With spyOn you can return mocked value and set it dynamically like in following code

                  it 'should place the component as last object in the form', ->
                    mockedValue = null
                  
                        spyOn(window, 'prompt').and.returnValue(mockedValue)
                        mockedValue = 'test'
                        builder.addFormObject 'default', {component: 'test'}
                  
                        mockedValue = 'test2'
                        builder.addFormObject 'default', {component: 'test2'}
                  
                        expect(builder.forms['default'][0].name).toEqual('test')
                  

                  这篇关于Jasmine spyOn 多次返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                  Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)

                      <i id='YQk0V'><tr id='YQk0V'><dt id='YQk0V'><q id='YQk0V'><span id='YQk0V'><b id='YQk0V'><form id='YQk0V'><ins id='YQk0V'></ins><ul id='YQk0V'></ul><sub id='YQk0V'></sub></form><legend id='YQk0V'></legend><bdo id='YQk0V'><pre id='YQk0V'><center id='YQk0V'></center></pre></bdo></b><th id='YQk0V'></th></span></q></dt></tr></i><div id='YQk0V'><tfoot id='YQk0V'></tfoot><dl id='YQk0V'><fieldset id='YQk0V'></fieldset></dl></div>
                        <bdo id='YQk0V'></bdo><ul id='YQk0V'></ul>
                        • <small id='YQk0V'></small><noframes id='YQk0V'>

                          <legend id='YQk0V'><style id='YQk0V'><dir id='YQk0V'><q id='YQk0V'></q></dir></style></legend>
                        • <tfoot id='YQk0V'></tfoot>
                            <tbody id='YQk0V'></tbody>