使用 Zombie 测试 Node.js 应用程序

发布于 2024-12-27 06:41:50 字数 818 浏览 1 评论 0原文

我正在使用express框架在coffee-script中编写一个node.js应用程序。在探索了几个选项之后,我最终决定使用 mocha 和 Zombie.js。然而,我正在努力测试用户界面。例如,要实现成功的用户身份验证,我执行以下操作:请参阅此处粘贴的代码 my_gist

我真正要做什么想要做的事情如下:

  • 调用 get '/sessions/new',它将调用 SessionsController 并显示身份验证表单,
  • 然后我将调用browser.visit方法,输入字段值并提交表单,这将生成一个post方法
  • 如果用户名和密码正确,我预计 SessionsController 会做出相应反应并重定向到正确的页面。 不幸的是,每当我运行测试时,它都会抱怨 Zombie: require is not Defined ReferenceError: require is not Define。事实证明它不喜欢我的 /javascripts/app.js 中的两行

    <前><代码> 需要(“咖啡脚本”) 需要('./tfs.咖啡')

即使当我尝试在访问方法之后从浏览器中提取任何信息时,我也只是得到未定义的值。显然我的断言都没有得到检验。它刚刚通过测试。我做错了什么吗?有人使用 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 about Zombie: require is not defined ReferenceError: require is not defined. It turns out it doesn't like the two lines in my /javascripts/app.js

    
    require("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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤痕我心 2025-01-03 06:41:50

如果从浏览器加载 /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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文