如果我使用试图从容器中读取的npm包@inrupt/solid-client
为代码编写单元测试,我该如何模拟该容器?
从技术上讲,容器是一个常规资源,它包含的每个资源都有一个Thing,并且一个Things由容器自己指向它们的URL标识。
因此,下面的代码应该做到这一点:
// We're going to mock a Container at https://vincentt.inrupt.net/public/,
// that contains one Resource at https://vincentt.inrupt.net/public/child-one.ttl.
const containerUrl = "https://vincentt.inrupt.net/public/";
// Add a Thing for the child at
// https://vincentt.inrupt.net/public/child-one.ttl:
let childOneListing = createThing({ url: containerUrl + "child-one.ttl" });
childOneListing = addUrl(childOneListing, rdf.type, ldp.Resource);
childOneListing = addDatetime(childOneListing, dct.modified, new Date());
// Create a Thing identified by the Container's own URL,
// pointing to the Things representing its children:
let childrenIndex = createThing({ url: containerUrl });
childrenIndex = addUrl(childrenIndex, rdf.type, ldp.BasicContainer);
childrenIndex = addUrl(childrenIndex, rdf.type, ldp.Container);
// Add child-one.ttl to this index:
childrenIndex = addUrl(childrenIndex, ldp.contains, childOneListing);
// Now initialise a mock Resource for this Container for use in our unit test,
// and add the index and the child Resources' Things:
let mockContainer = mockSolidDatasetFrom(containerUrl);
mockContainer = setThing(mockContainer, childrenIndex);
mockContainer = setThing(mockContainer, childOneListing);
可以在此处找到包含上述代码的CodeSandbox:https://codesandbox.io/s/ancient-wood-3u8m3?fontsize=14&隐藏导航=1&主题=深色