0

I am new to testing and have never used MSpec. I looked at tutorials and the only examples is "lite", like 1 + 1 should be 2. I need to test this real method and I don't know where to start.

 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 is just a class with a lot of properties and that has an extension.

Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
Don Juan
  • 171
  • 2
  • 17

2 Answers2

3

The MSpec github repo has a pretty nice README that explains the basic syntax components of an MSpec test class and test case.

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

I won't fill in the details of your test, but I will show you the important parts to setup an mspec test.

[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;
}

You'll want to run these tests, so use the command-line tool

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

or one of the integrations

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

Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
0

Let me navigate to you with a real implementation. Let's assume you have a service called SiteService and it returns the current siteId (you have multiple siteIds for your application).

you want to write a test case, when requesting the current site it should return the site id definition in configuration.

you will need to create a test class (standard class file), let's give a meaningful name like "SiteServiceSpec.cs" Next, you will need to mock the ISiteConfiguration so that it can get the site id from the SiteConfiguration

public abstract class SiteServiceContext : WithFakes
{
    Establish context = () =>
    {
        var siteConfiguration = An<ISiteConfiguration>();
        siteConfiguration.WhenToldTo(x => x.Id)
            .Return(CurrentSiteId);

        Repository = An<IRepository<WebSite.Domain.Site.Site>>();

        SUT = new SiteService(siteConfiguration, Repository);
    };

    protected const short CurrentSiteId = 1;
    protected static SiteService SUT;
    protected static IRepository<WebSite.Domain.Site.Site> Repository;
}

Now, here comes the example of the test class.

[Subject(typeof(SiteService))]
public class When_requesting_current_site : SiteServiceContext
{
    It should_return_site_with_id_defined_in_configuration = () =>
        Result.Id.ShouldEqual(CurrentSiteId);

    Establish context = () =>
    {
        var site = An<WebSite.Domain.Site.Site>();
        site.WhenToldTo(x => x.Id)
            .Return(CurrentSiteId);

        Repository.WhenToldTo(x => x.GetById(CurrentSiteId))
            .Return(site);
    };

    Because of = () =>
        Result = SUT.GetCurrentSite();

    static WebSite.Domain.Site.Site Result;
}

I hope it will help you to get an idea of how it works. Besides, follow the structure provided by @anthony-mastrean