问题描述
我想测试用户界面的一个特定元素。要做到这一点,我需要特别的要求,我的后端与predefined数据作出响应,但所有其他请求应通过。以下是我如何做到这一点(咖啡更多可读性):
I'm trying to test one particular element of user interface. To do so I need particular request to my backend to respond with predefined data, but all other requests should pass through. Here's how I do it (coffee for more readability):
describe 'select2 combobox widget', ()->
httpBackendMock = () ->
angular.module 'httpBackendMock', ['ngMockE2E', 'fmAppApp']
.run ($httpBackend)->
dummyCategories = [
{id: 1, name: 'foo', icon: '', user_id: 5},
{id: 2, name: 'bar', icon: '', user_id: 5},
{id: 3, name: 'baz', icon: '', user_id: 5},
{id: 4, name: 'quax', icon: '', user_id: 5}
]
$httpBackend.whenGET '/api/categories'
.respond ()->
[200, dummyCategories]
$httpBackend.whenGET /.*/
.passThrough()
$httpBackend.whenGET /^\/views\//
.passThrough()
$httpBackend.whenGET /^\/scripts\/.*/
.passThrough()
$httpBackend.whenGET /^\/scripts\//
.passThrough()
$httpBackend.whenGET /^\/bower_components\//
.passThrough()
$httpBackend.whenGET(/\.html$/).passThrough()
browser
.addMockModule 'httpBackendMock', httpBackendMock
所以基本上我做什么,这里是我的应用程序模块之上 fmAppApp
和角创建新的模块 ngMockE2E
和告诉量角器一下。
so basically what I do here is create new module on top of my application module fmAppApp
and angular ngMockE2E
and tell Protractor about it.
和对完整性的考虑,我将展示这里面这里简单语句描述
块:
And for the sake of completeness I'll show here one simple statement inside this describe
block:
it 'should allow user to type in anything', ()->
browser.get 'http://localhost:9000/#/'
element By.model 'category.selected'
.click()
input = element By.css '.ui-select-search'
input.sendKeys 'newtestcategory'
expect input.getAttribute('value')
.toBe 'newtestcategory'
当我运行繁重的量角器它会打开浏览器,导航到指定的网址(的http://本地主机:9000 /#/
),因为它应该,然后我看到空白页及规格与此错误失败: NoSuchElementError:没有使用定位器找到的元素:by.model(category.selected)
when I run grunt protractor it opens browser, navigates to specified url (http://localhost:9000/#/
) as it should and then I see blank page and spec failures with this error:NoSuchElementError: No element found using locator: by.model("category.selected")
不幸的消息是一切,因为我不能打开Firebug,看看什么地方出错了,原因很明显我有。我想我能以某种方式从重定向浏览器控制台日志记录和看到的是万恶之源,但在这里,我不知道怎么办。也许有人遇到也和知道这可能是什么?
更新:
我照Cétia以下建议,我得到这个消息在日志中:
Unfortunately this message is all I have since I can't open firebug and see what went wrong for obvious reasons. I guess I could redirect logging from browser console somehow and see what is root of evil here but I don't know how. Maybe someone encountered that as well and knows what it might be?Update:I did as Cétia below suggested and I got this message in logs:
消息:HTTP://本地主机:9000 / bower_components /角/ angular.js 11607:24错误:意外的请求:POST / API /登录
现在这是为什么?我要补充API /登录到直通()?
Now why is that? Should I add 'api/login' to passThrough() ?
推荐答案
看来我并没有提供足够的信息。我留下了一些code,我认为是无关紧要的意图不会打击你,但事实证明这是一个绊脚石。
It seems I didn't provide sufficient information. I left out some code that I thought was irrelevant with intent to not overwhelm you, but as it turned out it was a stumbling block.
browser.get 'http://localhost:9000/#/'
element By.model 'email'
.sendKeys 'dima@mail'
element By.model 'password'
.sendKeys '123456'
element By.cssContainingText 'form[name=login] button', 'Войти'
.click()
这code是负责记录用户的,我在这里需要它,因为未经授权的用户不能看到我想测试的看法。
我以为我确信全部请求让它通过,但当然,我忘了,我的登录请求是一个上传请求。所以,问题是由添加简单地加以解决。
This code is responsible for logging user in, I need it here because unauthorized users aren't allowed to see the view I wanted to test.I thought I made sure all requests make it through, but of course I forgot that my login request is a POST request. So problem was solved simply by adding
$httpBackend.whenPOST /.*/
.passThrough()
这篇关于当我嘲笑后端视图不加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!