【发布时间】:2016-09-18 07:08:47
【问题描述】:
我想问学生一个问题,定义算子的排名(+、/、-、*、^)。
我使用的方法要求我使用exports.function_name = function() 为了使规范文件能够导入它。
但是,我希望学生不必使用 export.function,因为这不是他们在编辑器中要做的。
因此,我需要找到一种有效的方法来包装学生输入的代码。这就是我目前所拥有的(src 文件:../src/C3Q2.js):
exports.StudentSolution = function()
{
var operators = {
"^": 4,
"*": 3,
"/": 2,
"+": 1,
"-": 0,
};
return operators;//This works but I dont want the student to do this
//extra step, the only thing they should have to do is define the variable
//operators
};
我正在测试src文件的文件如下(spec文件):
var calculator = require("../src/C3Q2.js");
describe("precedence", function ()
{
it("check precedence", function ()
{
var solution = calculator.StudentSolution();
var product = solution["^"];
expect(product).toBe(4);
});
});
这可行,但我希望学生只需要定义操作符变量。有没有更有效的方法来自动化和包装函数?
【问题讨论】:
-
我可以建议您仅 使用 UpperCamelCase 来表示类(结合
new关键字)?
标签: javascript node.js unit-testing jasmine