小编aaa*_*dan的帖子

在运行测试之前如何从mocha上的文件中读取?

我目前正在对API运行测试 - 使用Mocha没有问题.测试数组存储在变量中 - 文件顶部的"tests".我想从文本文件中读取测试信息,并在运行测试之前将信息解析为变量(一次).

我试过同步和异步使用before()(下面)

//Synchronously
describe("API Tests", function (done) {
    before(function(){
        tests = fs.readFileSync('./json.txt', 'utf8');
        tests = JSON.parse(tests);
    });
    for (var i = 0; i < tests.length; i++) {
        runTest(tests[i]);
    }
    done();
});
Run Code Online (Sandbox Code Playgroud)
//Asynchronously
describe("API Tests", function () {
var tests = "";
before(function(){
    fs.readFile('./json.txt', 'utf8', function(err, fileContents) {
    if (err) throw err;
    tests = JSON.parse(fileContents);
    });
});

for (var i = 0; i < tests.length; i++) {
    runTest(tests[i]);
}});
Run Code Online (Sandbox Code Playgroud)

节点返回一个错误,指出该文件不存在(它确实存在).

另外,我试图运行文件读取(同步和异步),在回调时执行封装描述(如下所示).似乎无法检测到案件,返回"未找到任何测试".

var tests;
fs.readFile('./json.txt', 'utf8', function(err, fileContents) …
Run Code Online (Sandbox Code Playgroud)

javascript testing file mocha.js node.js

3
推荐指数
1
解决办法
3329
查看次数

如果履行承诺,你如何才能执行?

如果某些函数没有按要求执行,我正在使用promises强制NodeJS停止运行.目前,服务器根据需要停止,但如果功能成功履行了承诺,我还想包含控制台日志.我正在使用npm'q'模块.

工作代码

Q.all([
    someFunction1(),
    someOtherFunction('https://www.google.com', 'Google'),
    someOtherFunction('https://www.facebook.com', 'Facebook'),
])
    .catch(function (err){
        console.log(err);
        process.exit(1);
})
Run Code Online (Sandbox Code Playgroud)

当按照以下方式添加then时,则在promises完成之前执行,因此无论promise是否已满足,都会执行console.log调用.

Q.all([
    someFunction1(),
    someOtherFunction('https://www.google.com', 'Google'),
    someOtherFunction('https://www.facebook.com', 'Facebook'),
])
    .then(console.log("No problem here"))
    .catch(function (err){
        console.log(err);
        process.exit(1);
})
Run Code Online (Sandbox Code Playgroud)

javascript node.js promise q

3
推荐指数
1
解决办法
66
查看次数

实例如何包含其他类的多个实例?

我正在创建一个基本游戏来帮助理解课程,Player()实例(Ben)拥有许多建筑物和单元.每个玩家,如本将拥有各种建筑物,如飞机工厂或Bot工厂.

class Player:

    def __init__(self):
        self.money = 200
        self.score = 0
        print('Created ')

    def increase_money(self, amount):
        self.money += amount

    def decrease_money(self, amount):
        self.money -= amount

    def check_money(self):
        print(self.money)


class Building(Player):

    def __init__(self):
        self.level = 0
        self.upgrade_cost = 100
        print('created building')

    def upgrade_building(self):
        self.level += 1

    def check_building_level(self):
        print(self.level)

ben = Player()
Run Code Online (Sandbox Code Playgroud)

我已经意识到,如果我调用Building()类来创建一个实例,它实际上会创建一个继承了播放器属性的构建实例(即每个构建都有自己的钱).

我如何让每个玩家包含不同属性的各种建筑物和单位?例如,本和比尔有不同的建筑物.在这种情况下,我会使用单个Player()类然后使用里面的函数Player()吗?

python oop inheritance class

2
推荐指数
1
解决办法
609
查看次数

标签 统计

javascript ×2

node.js ×2

class ×1

file ×1

inheritance ×1

mocha.js ×1

oop ×1

promise ×1

python ×1

q ×1

testing ×1