0

我是测试新手,从未使用过 MSpec。我查看了教程,唯一的例子是“lite”,比如1 + 1 should be 2. 我需要测试这个真正的方法,我不知道从哪里开始。

 public ILineItem CreateLineItem(BaseVariationContent sku, int quantityToAdd)
 {
    var price = sku.GetDefaultPrice();
    var parent = sku.GetParentProducts().FirstOrDefault() != null ? _contentLoader.Get<ProductContent>(sku.GetParentProducts().FirstOrDefault()).Code : string.Empty;

    return new LineItem
       {
          Code = sku.Code,
          DisplayName = sku.DisplayName,
          Description = sku.Description,
          Quantity = quantityToAdd,
          PlacedPrice = price.UnitPrice.Amount,
          ListPrice = price.UnitPrice.Amount,
          Created = DateAndTime.Now,
          MaxQuantity = sku.MaxQuantity ?? 100,
          MinQuantity = sku.MinQuantity ?? 1,
          InventoryStatus = sku.TrackInventory ? (int)InventoryStatus.Enabled : (int)InventoryStatus.Disabled, 
          WarehouseCode = string.Empty, // TODO: Add warehouse id
          ParentCatalogEntryId = parent,
       };
 }

BaseVariationContent只是一个具有很多属性并且具有扩展名的类。

4

1 回答 1

3

MSpec github repo 有一个非常好的 README,它解释了 MSpec 测试类和测试用例的基本语法组件。

https://github.com/machine/machine.specifications#machinespecifications

我不会填写您的测试的详细信息,但我会向您展示设置 mspec 测试的重要部分。

[Subject("Line Item")]
public class When_creating_a_basic_line_item_from_generic_sku()
{
    Establish context = () => 
    {
        // you would use this if the Subject's constructor
        // required more complicated setup, mocks, etc.
    }

    Because of = () => Subject.CreateLineItem(Sku, Quantity);

    It should_be_in_some_state = () => Item.InventoryStatus.ShouldEqual(InventoryStatus.Enabled);

    private static Whatever Subject = new Whatever();
    private static BaseVariationContent Sku = new GenericSku();
    private static int Quantity = 1;
    private static ILineItem Item;
}

您需要运行这些测试,因此请使用命令行工具

https://github.com/machine/machine.specifications#command-line-reference

或其中一种集成

https://github.com/machine/machine.specifications#resharper-integration

于 2015-11-16T23:28:28.820 回答