Skip to main content

Posts

Showing posts from March, 2018

Test-driven development (TDD) with Mspec - Part 2

This post is the second in the series of posts regarding implementing Mspec for unit tests within TDD. This section looks at how tests can be written to check that the correct exceptions are thrown under certain conditions. An understanding of  Test-driven development (TDD) with Mspec - Part 1 is assumed. The test shown  here will cover the throwing of a validation exception when a user is added without a name. New users are added using an AddUserModel , which is shown below. Note that the Required  tag is appended to both the Name  and Age  properties. In the post method in the user controller, first the model state is checked to ensure it is valid. If the model is not valid (i.e if there is no value for either the age or name, or both) a validation exception is thrown. In the test an AddUserModel  is created with a null name in the Establish  step. Then, in the Because  step the result is set to the output of the post method. Howe...

Test-driven development (TDD) with Mspec - Part 1

This post describes how to use MSpec tests to implement test-driven development (TDD). Details on TDD can be found on  this  wikipedia page. Machine.specifications is simple to install through the Nuget package manager, more information can be found on the  MSpec site . Here Machine Fakes is also used to create the unit tests, more information on this can be found here . This blog post will be analogous to Behaviour-driven development (BDD) with SpecFlow - Part 1 , hence a direct comparison is able to be made between the two testing methodologies. The example below will show the Mspec test for the command query that gets all users from the database. By convention Mspec classes are titled in snake case. The Subject attribute is added, although this is not essential it is good practice to do so. The test class inherits from WithSubject (from Machine.Fakes) with the type being the class where the testable code resides. Note : the following code snippets are all cont...