使用 Zombie 测试 Node.js 应用程序
我正在使用express框架在coffee-script中编写一个node.js应用程序。在探索了几个选项之后,我最终决定使用 mocha 和 Zombie.js。然而,我正在努力测试用户界面。例如,要实现成功的用户身份验证,我执行以下操作:请参阅此处粘贴的代码 my_gist
我真正要做什么想要做的事情如下:
- 调用
get '/sessions/new'
,它将调用 SessionsController 并显示身份验证表单, - 然后我将调用
browser.visit
方法,输入字段值并提交表单,这将生成一个post方法 如果用户名和密码正确,我预计 SessionsController 会做出相应反应并重定向到正确的页面。 不幸的是,每当我运行测试时,它都会抱怨
<前><代码> 需要(“咖啡脚本”) 需要('./tfs.咖啡')Zombie: require is not Defined ReferenceError: require is not Define
。事实证明它不喜欢我的 /javascripts/app.js 中的两行
即使当我尝试在访问方法之后从浏览器中提取任何信息时,我也只是得到未定义的值。显然我的断言都没有得到检验。它刚刚通过测试。我做错了什么吗?有人使用 Zombie.js 测试咖啡脚本编写的应用程序时遇到过这个问题吗?解决办法是什么?
I am writing a node.js app in coffee-script using the express framework. After exploring a couple of options I finally decided to use mocha and zombie.js. However, I am having a hard testing the UI. For example, to implement a successful user authentication I do the following: see the code pasted here my_gist
What I really wanted to do is the following:
- call
get '/sessions/new'
, which will call the SessionsController and display the authentication form - then I'll call the
browser.visit
method, enter the values for the fields and submit the form, which will generate a post method if the username and password are correct, I'll expect the SessionsController to react accordingly and redirect to the right page.
Unfortunately, whenever I run the tests it complains aboutZombie: require is not defined ReferenceError: require is not defined
. It turns out it doesn't like the two lines in my /javascripts/app.jsrequire("coffee-script") require('./tfs.coffee')
Even when I try to extract any information from the browser after the visit method, I just get undefined values. Apparently none of my assertions is being tested. It just passes the test. Is there anything I am doing wrong? Has anyone testing coffee-script written app in express using Zombie.js faced that problem? what could be the fix?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果从浏览器加载 /javascripts/app.js,您需要确保首先加载 RequireJS(或定义
require
的其他浏览器框架),或者将它们加载到您的 HTML 文档中:<脚本 src="tfs.coffee" type="text/coffee">
您可能需要等到页面完全加载后再提取值。
If /javascripts/app.js is being loaded from the browser, you would want to make sure that RequireJS (or some other browser framework that defines
require
) is loaded first, or load them in your HTML document:<script src="coffee-script.js" type="text/javascript"></script>
<script src="tfs.coffee" type="text/coffee"></script>
You might need to wait until the page is fully loaded before extracting values.