Skip to content

Instantly share code, notes, and snippets.

View k0stya's full-sized avatar

Kostiantyn Khomenko k0stya

View GitHub Profile
@k0stya
k0stya / Test Doubles - Mock.cs
Last active December 14, 2015 02:38
Mock object replaces an object the system under test (SUT) depends on and verifies it is being used correctly by the SUT
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace TestDoubles.Examples.Mock
{
[TestClass]
public class Player_Test
{
[TestMethod]
public void Should_be_able_to_roll_die_wtih_max_face_value()
@k0stya
k0stya / Test Doubles - Stub.cs
Last active December 14, 2015 02:29
Stub replaces a real object and feeds the desired indirect inputs into the system under test.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace TestDoubles.Examples.Stub
{
[TestClass]
public class Player_Test
{
[TestMethod]
public void Should_be_able_to_roll_die_wtih_max_face_value()
@k0stya
k0stya / Test doubles - dummy object.cs
Last active December 14, 2015 02:19
Dummy is a placeholder required to pass the unit test. Unit in the context (SUT) doesn't exercise this placeholder. Dummy can be something as simple as passing ‘null’ or a void implementation with exceptions to ensure it’s never leveraged.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace TestDoubles.Examples.Dummy
{
[TestClass]
public class Player_Test
{
[TestMethod]
public void Should_be_able_to_roll_die_wtih_max_face_value()
@k0stya
k0stya / SetupControllerForTests.cs
Last active December 12, 2015 10:39
Helper method that allows to setup controller class for unit testing.
private void SetupControllerForTests(ApiController controller, string controllerName)
{
var config = new HttpConfiguration();
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/" + controllerName);
var route = config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", controllerName } });
controller.ControllerContext = new HttpControllerContext(config, routeData, request);
controller.Request = request;
controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
@k0stya
k0stya / AspNetDevelopmentServerAttribute Example.cs
Last active December 12, 2015 10:39
Helper class that allows to do integration testing of WCF services using AspNetDevelopmentServerAttribute.
// Example of usage
[TestMethod]
[AspNetDevelopmentServer("WcfService", "Physical path to your WCF project")]
public void TestMethod1()
{
ServiceClient target = new ServiceClient();
Assert.IsTrue(WcfWebServiceHelper.TryUrlRedirection(target, TestContext, "WcfService"));
int value = 0;
string expected = "You entered: 0";
string actual;