Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 测试投掷错误_Javascript_Node.js_Unit Testing_Mocha.js_Chai - Fatal编程技术网

Javascript 测试投掷错误

Javascript 测试投掷错误,javascript,node.js,unit-testing,mocha.js,chai,Javascript,Node.js,Unit Testing,Mocha.js,Chai,我在试着测试投掷的错误 这是我的密码 const validatorMethod = (data) => { const validationResult = Object.keys(data) .map((key) => { if (!data[key] || data[key].trim() === '') { return key; } return true; }); if (validationRe

我在试着测试投掷的错误

这是我的密码

const validatorMethod = (data) => {
  const validationResult = Object.keys(data)
    .map((key) => {
      if (!data[key] || data[key].trim() === '') {
        return key;
      }
      return true;
    });
  if (validationResult.filter((prop) => prop === true).length !== data.length) {
    return validationResult.filter((prop) => prop !== true);
  }
  return true;
};

module.exports = {
  userObjectFactory (data) {
    console.log(data);
    const invalidKeys = validatorMethod(data);
    if (invalidKeys.length === true) {
      console.log(1);
      return data;
    }
    console.log(2);
    throw new Error('One of passed properties is empty');
  },
};
这是我的测验

const userTemplate = {
  id: 1,
  email: 'a@a.a',
  password: 'zaq1@WSX',
  fullName: 'full name',
  location: 'location',
  isLookingForWork: false,
};
describe('factory should throw error on undefined, null, or ""', () => {
  it('should throw an error if some inputs are undefined', () => {
    const userWithUndefinedProperty = userTemplate;
    userWithUndefinedProperty.id = undefined;
    userWithUndefinedProperty.password = undefined;

    assert.throws(
      userObjectFactory(
        userWithUndefinedProperty, new Error('One of passed properties is empty'), // also tried "Error" and "Error('One of passed properties is empty')" without the "new"
      ),
    );
  });
});
输出

  0 passing (68ms)
  2 failing

  1) testing UserObjectFactory
       should return an object with correct data:
     Error: One of passed properties is empty
      at userObjectFactory (src\user\UserObjectFactory.js:2:1646)
      at Context.it (test\user\UserObjectFactory.test.js:33:18)

  2) testing UserObjectFactory
       factory should throw error on undefined, null, or ""
         should throw an error if some inputs are undefined:
     Error: One of passed properties is empty
      at userObjectFactory (src\user\UserObjectFactory.js:2:1646)
      at Context.it (test\user\UserObjectFactory.test.js:26:9)

您应该传递给
assert.throws
一个函数的引用,该函数在被调用时会抛出错误

e、 g


结果如何?有几件事:1。您通常需要向检查错误的方法传递一个可调用的;二,。您正在将错误作为第二个参数传递给工厂,而不是
.throws
.1。什么意思?2.你说得对,谢谢,但纠正这一点并不能解决问题阅读文档:。您需要传递在调用时应该抛出错误的内容,以推迟执行,否则在调用
assert.throws
之前抛出错误。再说一次,你发布的内容的结果是什么?@jornsharpe我在我的代码中发现了许多小错误,并且正在修复它们,但是它仍然抛出错误,因此它应该断言它是真的。我正在添加output@jonrsharpe如果需要将值传递到方法中,如何在不调用它的情况下传递它?
const thisIsAFunctionThatIsSupposedToThrowWhenCalled = () =>
  userObjectFactory(
    userWithUndefinedProperty,
    new Error("One of passed properties is empty") // also tried "Error" and "Error('One of passed properties is empty')" without the "new"
  );

assert.throws(thisIsAFunctionThatIsSupposedToThrowWhenCalled);
assert.throws(
  () => userObjectFactory(
    userWithUndefinedProperty, new Error('One of passed properties is empty'), // also tried "Error" and "Error('One of passed properties is empty')" without the "new"
  )
)