如何在 Hyperledger Composer 中通过单元测试引用一系列产品
How to reference to an array of products with unit-test in Hyperledger Composer
我的货物有一系列产品,但我不明白如何参考这些产品。我正在尝试设置单元测试环境并需要提供测试数据。
装运模块
asset Shipment identified by shipmentId{
o String shipmentId
--> Product[] allProducts
--> participant owner
}
和单元测试
const shipment = factory.newResource(namespace, 'Shipment', '001');
shipment.allProducts = factory.newRelationship(namespace, participant, /** what to do here */ )
或者我可以只传递一系列产品,例如:
shipment.allProducts = products
我之所以怀疑这个解决方案是因为我需要使用工厂 newRelationship 函数。如果您有建议,那将很有帮助。
好吧,我终于找到了解决办法。我的错误信息是:
Instance org.trader.network.Shipment#001 has property allProducts with type org.trader.network.Product that is not derived from org.trader.network.Product[]
如果您想进行单元测试并将一些数据添加到引用的数组类型,请执行以下操作:
shipment.allProducts= [factory.newRelationship(
namespace,
'Product',
product.$identifier
)];
注意工厂周围的括号。我需要为产品建立一系列关系。
我的单元测试只需要一个产品,但如果您想要更多,只需在括号之间添加产品的更多关系即可。
我通过随机查看 github 存储库找到了解决方案(来源:here)
我的货物有一系列产品,但我不明白如何参考这些产品。我正在尝试设置单元测试环境并需要提供测试数据。
装运模块
asset Shipment identified by shipmentId{
o String shipmentId
--> Product[] allProducts
--> participant owner
}
和单元测试
const shipment = factory.newResource(namespace, 'Shipment', '001');
shipment.allProducts = factory.newRelationship(namespace, participant, /** what to do here */ )
或者我可以只传递一系列产品,例如:
shipment.allProducts = products
我之所以怀疑这个解决方案是因为我需要使用工厂 newRelationship 函数。如果您有建议,那将很有帮助。
好吧,我终于找到了解决办法。我的错误信息是:
Instance org.trader.network.Shipment#001 has property allProducts with type org.trader.network.Product that is not derived from org.trader.network.Product[]
如果您想进行单元测试并将一些数据添加到引用的数组类型,请执行以下操作:
shipment.allProducts= [factory.newRelationship( namespace, 'Product', product.$identifier )];
注意工厂周围的括号。我需要为产品建立一系列关系。
我的单元测试只需要一个产品,但如果您想要更多,只需在括号之间添加产品的更多关系即可。
我通过随机查看 github 存储库找到了解决方案(来源:here)