Skip to content

Instantly share code, notes, and snippets.

View k0stya's full-sized avatar

Kostiantyn Khomenko k0stya

View GitHub Profile
@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;
@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 / 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 / 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 - 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 - Fake.cs
Last active December 14, 2015 03:09
Replace a component that the system under test (SUT) depends on with a much lighter-weight implementation.
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestDoubles.Examples.Fake
{
[TestClass]
public class Invoice_Test
{
[TestMethod]
@k0stya
k0stya / Test doubles - Test-Specific Object - testing protected members.cs
Last active December 14, 2015 03:09
If the system under test (SUT) was not designed specifically to be testable, we may find that the test cannot get access to state that it must initialize or verify at some point in the test.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace TestDoubles.Examples.TestSpecificSubclass.ProtectedMember
{
[TestClass]
public class Player_Test
{
[TestMethod]
public void Should_be_able_to_roll_die_wtih_max_face_value()
[TestMethod]
public void $MethodUnderTest$_should_$EXPECTED_BEHAVIOR$()
{
// Arrange
$END$
// Act
// Assert
}
@k0stya
k0stya / FalseWarning.cs
Last active December 14, 2015 08:48
Reshaper highlights n.Value saying that there is a possibility of InvalidOperationException.
using System.Linq;
namespace Nullable
{
public class Nullable_Test
{
public void SumNullableNumbers()
{
int?[] numbers = new int?[10];
int sum = numbers.Where(n => n.HasValue).Sum(n => n.Value);
@k0stya
k0stya / AssemblyGenerator.cs
Created March 25, 2013 19:54
Generate assembly at runtime
using System;
using System.Reflection;
using System.Reflection.Emit;
namespace ConsoleApplication1
{
public class AssemblyGenerator
{
public static ModuleBuilder CreateModuleBuilder(Action<ModuleBuilder> buildTypes, string assemblyName)
{