在Javascript中使用断言为NaN

时间:2018-03-17 05:07:33

标签: javascript node.js unit-testing

根据docs,我正在尝试使用assert.deepstrictEqual来测试我的输出为NaN。但测试不会通过

功能代码:

function howManyServings (params) {
  const {recipe, inventory} = params

  bread_confirm = Math.floor(inventory.bread / recipe.bread)
  peanutButter_confirm = Math.floor(inventory.peanutButter / recipe.peanutButter)
  jam_confirm = Math.floor(inventory.jam / recipe.jam)

  minimum_servings = Math.min(bread_confirm, peanutButter_confirm, jam_confirm)
  return minimum_servings
}

测试代码:

const assert = require('assert');
const {howManyServings} = require('./index');
const inputs = require('./inputs')

describe('Nomnomthrough', () => {
  describe('When there is enough ingredient for one serving', () => {
    it('should return 1', () => {
      const output = howManyServings(inputs.oneServing);
      assert.equal(output, 1);
    });
  });
  describe('When there is enough for two servings', () => {
    it('should return 2', () => {
        const output = howManyServings(inputs.twoServings);
        assert.equal(output, 2)
    })
  })
  describe('When the amount is not an integer', () => {
    it('should return false', () => {
        const output = howManyServings(inputs.nonintegerValue);
        assert.deepStrictEqual(output, NaN)
    })
  })
});

输入代码:

module.exports = {
  oneServing: {
    recipe: {
      bread: 10,
      peanutButter: 5,
      jam: 5
    },
    inventory: {
      bread: 20,
      peanutButter: 10,
      jam: 5
    }
  },

  twoServings: {
    recipe: {
      bread: 10,
      peanutButter: 5,
      jam: 5
    },
    inventory: {
      bread: 20,
      peanutButter: 10,
      jam: 10
    }
  },

  nonintegerValue: {
    recipe: {
      bread: 10,
      peanutButter: 5,
      jam: 5
    },
    inventory: {
      bread: 20,
      peanutButter: 10,
      jam: 'sfsf'
    }
  }
}

代码返回NaN,但我认为它NaN不会被deepStrictEqual认为是等于public void performFileSearch() { // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file // browser. Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); // Filter to only show results that can be "opened", such as a // file (as opposed to a list of contacts or timezones) intent.addCategory(Intent.CATEGORY_OPENABLE); // Filter to show only images, using the image MIME data type. // If one wanted to search for ogg vorbis files, the type would be "audio/ogg". // To search for all documents available via installed storage providers, // it would be "*/*". intent.setType("image/*"); startActivityForResult(intent, READ_REQUEST_CODE); }

1 个答案:

答案 0 :(得分:2)

要检查output变量NaN是否为assert(isNaN(output)) ,您可以使用isNaN()

assert.equal(isNaN(output), true)

或者,

assert.ok(isNaN(output))

或者,

NaN

docs中所述:

针对NaN

进行测试

==将不等(通过!====!==Number.isNaN())与任何其他值进行比较 - 包括另一个NaN值。使用isNaN()NaN === NaN; // false Number.NaN === NaN; // false isNaN(NaN); // true isNaN(Number.NaN); // true function valueIsNaN(v) { return v !== v; } valueIsNaN(1); // false valueIsNaN(NaN); // true valueIsNaN(Number.NaN); // true 可以最清楚地确定某个值是否为NaN。或者进行自我比较:NaN,只有NaN,将比较不等于自身。

if (ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.ACCESS_CORSE_LOCATION)
    != PackageManager.PERMISSION_GRANTED) {

    // Permission is not granted
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.ACCESS_CORSE_LOCATION)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed; request the permission
        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.ACCESS_CORSE_LOCATION},
                0x0);


    }
} else {
    // Permission has already been granted
}