import { Head } from 'mdx-deck' import { CodeSurfer } from "mdx-deck-code-surfer"
export { future as theme } from 'mdx-deck/themes'
<title>Unit Testing</title>Being able to correctly unit test your applications implies that your app is set up a certain way. If it isn't, then it immediately becomes much more difficult to actually write unit tests.
- Dependency Injection / IoC (Inversion of Control)
- Mocking library (Moq)
- Test Running framework (MSTest, NUnit, XUnit, etc)
- Inject important objects
- Better mocking
- Also makes code not in unit tests better
- Less manual configuration
- Only test objects/methods that need to be tested
- Can't run the tests you write if you don't have one!
The code for this project is an ASP.NET Web API 2.2 project, which is a .NET Framework 4.5+ web application.
It supplies a RESTful web service that serves json data.
- DI/IoC -> AutoFac
- Mocking Library -> Moq
- Test Runner -> MSTest
List of alternatives from Scott Hanselman https://bit.ly/2Esv9bq
<CodeSurfer title="Dependency Injection" code={require("!raw-loader!./snippets/AutoFacService.cs")} lang="clike" showNumbers={false} dark={false} steps={[ { }, { lines: [11, 12], notes: "We need to register the API Controllers" }, { lines: [20, 21], notes: "Registering types/objects is vital for dependency injection" }, { range: [23, 26] } ]} />
<CodeSurfer title="Global.asax.cs" code={require("!raw-loader!./snippets/Global.asax.cs")} lang="clike" showNumbers={true} dark={false} steps={[ {}, { lines: [5]} ]} />
<CodeSurfer title="AutoMapper" code={require("!raw-loader!./snippets/AutoMapper.cs")} lang="clike" showNumbers={false} dark={false} steps={[ {}, { range: [1, 17]}, { range: [19, 76] } ]} />
- Right-click Solution
▶️ Add New Project▶️ Visual C# ️️▶️ Test▶️ MSTest Test Project. - Name it similar to your main project/solution, but append .Tests at the end
- Add A reference to your main project
- Add Moq NuGet Package
- Add other (service) references your code may use (ie. web service)
<CodeSurfer code={require("!raw-loader!./snippets/ExControllerTests.cs")} lang="clike" showNumbers={false} dark={false} steps={[ {}, { range: [14, 27] }, { range: [29, 38] }, { range: [41, 72] }, ]} />