开玩笑刚刚通过了所有测试,即使它是错误的

时间:2019-10-17 10:49:54

标签: javascript reactjs vue.js jestjs

我正在用玩笑来测试我的vue.js应用。 我意识到开玩笑只是通过了所有这些测试,即使它应该失败。 例如,此输入应该失败,因为输入类型的密码为true,但是我所有的测试代码都通过了true或false。 ..

  test('password input field type is password at the beginning', done => {
    setTimeout(() => {
      const pw = wrapper.find({ ref: 'pw' });
      expect(pw.contains("[type='password']")).toBe(false);
    }, 10);
    done();
  });
beforeEach(() => {
  // Create the Store mock

  state = {
    auth: {
      something: false,

    },
    data: {},


  };
  getters = {
    somethingg: jest.fn(),

  };
  actions = {
    'auth/login': jest.fn(),

  };

  store = new Store({
    state,
    getters,
    actions
  });

  wrapper = shallowMount(TheLoginComponent, {
    propsData: {},
    mocks: {},
    stubs: {

    },
    methods: {

    },
    data: {},
    localVue,
    store
  });

});

afterEach(() => {
  wrapper.destroy();
});

1 个答案:

答案 0 :(得分:1)

在这种情况下,

setTimeout不适用于开玩笑。它是异步的,不能保证在玩笑完成之前就已执行。

如果您有异步调用使您可以编写它,请如下所示进行flushPromises并尝试:

var rule = FirewallManager.Instance.Rules.Where(o => o.Direction == FirewallDirection.Inbound && o.Name.Equals("Allow Remote Desktop") ).FirstOrDefault(); if (rule != null) { //Update an existing Rule rule.RemoteAddresses = new IAddress[] { SingleIP.Parse("192.168.184.1"), SingleIP.Parse("192.168.184.2") }; return; } //Create a new rule rule = FirewallManager.Instance.CreateApplicationRule( FirewallManager.Instance.GetProfile().Type, @"Allow Remote Desktop", FirewallAction.Allow, null ); rule.Direction = FirewallDirection.Inbound; rule.LocalPorts = new ushort[] { 3389 }; rule.Action = FirewallAction.Allow; rule.Protocol = FirewallProtocol.TCP; rule.Scope = FirewallScope.All; rule.Profiles = FirewallProfiles.Public | FirewallProfiles.Private; rule.RemoteAddresses = new IAddress[] { SingleIP.Parse("192.168.184.1") }; FirewallManager.Instance.Rules.Add(rule);

内部测试:(注意方法是异步的)

const flushPromises = new Promise(resolve=> setImmediate(resolve)) ;