Javascript 对象属性的茉莉花测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31528200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jasmine test for object properties
提问by sfletche
What I'd like to do
我想做什么
describe('my object', function() {
it('has these properties', function() {
expect(Object.keys(myObject)).toEqual([
'property1',
'property2',
...
]);
});
});
but of course Object.keys
returns an array, which by definition is ordered...I'd prefer to have this test pass regardless of property ordering (which makes sense to me since there is no spec for object key ordering anyway...(at least up to ES5)).
但当然Object.keys
返回一个数组,根据定义,它是有序的......我更愿意让这个测试通过而不考虑属性排序(这对我来说很有意义,因为无论如何都没有对象键排序的规范......(至少最高 ES5))。
How can I verify my object has all the properties it is supposed to have, while also making sure it isn't missing any properties, without having to worry about listing those properties in the right order?
如何验证我的对象具有它应该拥有的所有属性,同时确保它没有丢失任何属性,而不必担心以正确的顺序列出这些属性?
回答by Plato
It's built in now!
现在已经内置了!
describe("jasmine.objectContaining", function() {
var foo;
beforeEach(function() {
foo = {
a: 1,
b: 2,
bar: "baz"
};
});
it("matches objects with the expect key/value pairs", function() {
expect(foo).toEqual(jasmine.objectContaining({
bar: "baz"
}));
expect(foo).not.toEqual(jasmine.objectContaining({
c: 37
}));
});
});
Alternatively, you could use external checks like _.has(which wraps myObject.hasOwnProperty(prop)
):
或者,您可以使用像_.has这样的外部检查(包装myObject.hasOwnProperty(prop)
):
var _ = require('underscore');
describe('my object', function() {
it('has these properties', function() {
var props = [
'property1',
'property2',
...
];
props.forEach(function(prop){
expect(_.has(myObject, prop)).toBeTruthy();
})
});
});
回答by Jordan Running
The simplest solution? Sort.
最简单的解决方案?种类。
var actual = Object.keys(myObject).sort();
var expected = [
'property1',
'property2',
...
].sort();
expect(actual).toEqual(expected);
回答by user1907116
it('should contain object keys', () => {
expect(Object.keys(myObject)).toContain('property1');
expect(Object.keys(myObject)).toContain('property2');
expect(Object.keys(myObject)).toContain('...');
});
回答by Richard Czechowski
I ended up here because I was looking for a way to check that an object had a particular subset of properties.
I started with _.has
or Object.hasOwnProperties
but the output of Expected false to be truthy
when it failed wasn't very useful.
我最终来到这里是因为我正在寻找一种方法来检查对象是否具有特定的属性子集。我从_.has
or开始,Object.hasOwnProperties
但是Expected false to be truthy
失败时的输出不是很有用。
Using underscore's intersection gave me a better expected/actual output
使用下划线的交集给了我更好的预期/实际输出
var actualProps = Object.keys(myObj); // ["foo", "baz"]
var expectedProps =["foo","bar"];
expect(_.intersection(actualProps, expectedProps)).toEqual(expectedProps);
In which case a failure might look more like
Expected [ 'foo' ] to equal [ 'foo', 'bar' ]
在这种情况下,失败可能看起来更像是
Expected [ 'foo' ] to equal [ 'foo', 'bar' ]
回答by Avram Virgil
I am late to this topic but there is a a method that allows you to check if an object has a property or key/value pair:
我在这个话题上迟到了,但是有一个方法可以让你检查一个对象是否有一个属性或键/值对:
expect(myObject).toHaveProperty(key);
expect({"a": 1, "b":2}).toHaveProperty("a");
or
或者
expect(myObject).toHaveProperty(key,value);
expect({"a": 1, "b":2}).toHaveProperty("a", "1");
回答by Kevin Mendez
I prefer use this; becouse, you have more possibilities to be execute indivual test.
我更喜欢用这个;因为,您有更多的可能性来执行单独的测试。
import AuthRoutes from '@/router/auth/Auth.ts';
describe('AuthRoutes', () => {
it('Verify that AuthRoutes be an object', () => {
expect(AuthRoutes instanceof Object).toBe(true);
});
it("Verify that authroutes in key 'comecios' contains expected key", () => {
expect(Object.keys(AuthRoutes.comercios)).toContain("path");
expect(Object.keys(AuthRoutes.comercios)).toContain("component");
expect(Object.keys(AuthRoutes.comercios)).toContain("children");
expect(AuthRoutes.comercios.children instanceof Array).toBe(true);
// Convert the children Array to Object for verify if this contains the spected key
let childrenCommerce = Object.assign({}, AuthRoutes.comercios.children);
expect(Object.keys(childrenCommerce[0])).toContain("path");
expect(Object.keys(childrenCommerce[0])).toContain("name");
expect(Object.keys(childrenCommerce[0])).toContain("component");
expect(Object.keys(childrenCommerce[0])).toContain("meta");
expect(childrenCommerce[0].meta instanceof Object).toBe(true);
expect(Object.keys(childrenCommerce[0].meta)).toContain("Auth");
expect(Object.keys(childrenCommerce[0].meta)).toContain("title");
})
});
回答by chrisjlee
Here are some new possible solutions too:
这里也有一些新的可能的解决方案:
- There's a module for that: https://www.npmjs.com/package/jasmine-object-matchers
- With ES2016, you can use a map and convert the object to a map too
- 有一个模块:https: //www.npmjs.com/package/jasmine-object-matchers
- 使用 ES2016,您可以使用地图并将对象也转换为地图