刚开始使用Jasmine并按照the Jasmine website上的说明进行异步测试时,我注意到从未调用过beforeEach函数。
知道为什么不呢?我在网上的任何地方都找不到任何相关提示。感谢。
代码
代码非常简单:
describe("Testing test.php", function()
{
it ("Gets me a coke", function()
{
var asyncResult = null;
// query function with callback
var queryFcn = function(callback)
{
console.log("queryFcn");
$.get("be_com/test.php?coke")
.success(function(data)
{
asyncResult = data.response;
callback(); // notify jasmine
})
.error(function() { callback(); });
};
// Call ajax
beforeEach(function(done) {
console.log('beforeEach');
queryFcn(done); });
// Evaluate response
expect(asyncResult).toBe("a can of coke");
});
});
HTML同样简单:
<html>
<head>
<link rel="shortcut icon" type="image/png" href="../fe_com/js/lib/jasmine-standalone-2.3.4/lib/jasmine-2.3.4/jasmine_favicon.png">
<link rel="stylesheet" type="text/css" href="../fe_com/js/lib/jasmine-standalone-2.3.4/lib/jasmine-2.3.4/jasmine.css">
<script type="text/javascript" src="../fe_com/js/lib/jquery-2.0.0.min.js"></script>
<script type="text/javascript" src="../fe_com/js/lib/jasmine-standalone-2.3.4/lib/jasmine-2.3.4/jasmine.js"></script>
<script type="text/javascript" src="../fe_com/js/lib/jasmine-standalone-2.3.4/lib/jasmine-2.3.4/jasmine-html.js"></script>
<script type="text/javascript" src="../fe_com/js/lib/jasmine-standalone-2.3.4/lib/jasmine-2.3.4/boot.js"></script>
<!-- FILES -->
<script type="text/javascript" src="testSpec.js"></script>
</head>
<body>
<h2>Unit Tests</h2>
</body>
</html>
答案 0 :(得分:2)
beforeEach
块不应位于it
块内。
beforeEach
的想法是,其中的代码在以下每个it
块之前运行。例如:
beforeEach(function () {
// code in here will run 3 times
});
it('does first thing', function () {});
it('does a second thing', function () {});
it('does a third thing', function () {});