我刚开始使用 Laika 在我的 Meteor 应用程序上做一些 TDD。不过,我想做一些集成测试,因为单元测试对我来说没那么有值(value)。
我可以通过 Laika 使用 PhantomJS 进行一些屏幕捕获吗?例如。我想单击 html 链接并按类/id 选择元素。
我有一个关于咖啡的基本(单元)测试:
# tests/players_test.coffee
assert = require 'assert'
suite 'Players', ->
test 'in the server', (done, server) ->
server.eval ->
Players.insert title: 'hello there'
players = Players.find().fetch()
emit('players', players)
server.once 'players', (players) ->
assert.equal 1, players.length
done()
我想通过使用客户端(在测试函数中添加到 (done, server)
旁边)将其转换为集成测试,然后手动选择标签并单击链接,填写名称等等,点击例如'注册',然后检查是否在数据库中找到该用户。
谢谢!
最佳答案
是的,你可以做到这一点。
suite 'Players', ->
test 'in the server', (done, server, client) ->
client.eval ->
// get access to a DOM element (can optionally use jQuery instead)
player = document.querySelector("[data-test='player']")
// now we can call functions on the element
player.value = "Joe blogs"
player.click()
// if you know the element won't exist in the DOM yet use waitForDOM
waitForDOM "[data-test='something-else']", ->
// perform some logic now that the element exists in the DOM
emit('players', players)
server.once 'players', (players) ->
assert.equal 1, players.length
done()
您可能还想在这里查看 evalSync:http://arunoda.github.io/laika/syntax-sugar.html
这让您可以以同步方式编写测试。它仍然是异步执行的,但这意味着您不必为“评估”测试所具有的所有不同的命名触发器/订阅而烦恼。以下是 evalSync 的基础知识...
suite 'evalSync', ->
test 'evalSync for the win', (done, server, client) ->
client.evalSync ->
// perform some logic on the client
emit("return")
server.evalSync ->
// perform some logic on the server
emit("return")
done() // notice this is outside the 'evalSync'
作为旁注,我建议在您的元素(或其他一些自定义数据属性)上使用“data-test”属性。如果您在测试中按类或 ID 选择,然后重构您的 CSS/HTML,您将不得不找出哪些类/ID 正在被您的 CSS 使用,哪些正在被您的测试使用。使用“数据测试”让您一目了然。
关于testing - 使用 Laika 和 PhantomJS 进行集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20998919/