mardi 23 février 2016

How to create a unit test for a void Create/Update method that calls a DAL method to do the actual adding/updating?

I have 2 methods, Create(Item item) and Update(Item item), that I want to create a unit test for. The scenario I want to test is if the parameter "item" has valid values, it gets created/updated. So the code right now looks like this:

public void Create(Item item)
{
    // Code here for validation. If valid values, create item.
    _DAL.Create(item)
}

public void Update(Item item)
{
    // Code here for validation. If valid values, update item.
    _DAL.Update(item)
}

Now I want to create a unit test for this for successful creation/update. I was thinking I check if a new record was added/updated but I don't know how to go about it.

[Fact]
public void Create_ValidValues_CreatesItem()
{
    // Arrange.
    ItemService itemService = new ItemService();
    Item item = new Item();

    // Act.
    itemService.Create(item);

    // Assert.
    // What do I assert?
}

I want to mock the database manually (no framework) and check if item is created or updated by checking number of records for create and values of properties for update. How can I do this? I am using xunit but I think any framework will do; I just need to understand how it is done.

Aucun commentaire:

Enregistrer un commentaire