Skip to content

Instantly share code, notes, and snippets.

View mauricedb's full-sized avatar

Maurice de Beijer mauricedb

View GitHub Profile
@mauricedb
mauricedb / gist:5280482
Last active December 15, 2015 15:19
AngularJS controller using TypeScript
/// <reference path="../Scripts/typings/angularjs/angular.d.ts" />
module App {
export class TestCtrl {
private startTime: string;
constructor(private $scope: ITestCtrlScope) {
this.startTime = new Date().toLocaleString();
$scope.message = "Hello there at " + this.startTime;
@mauricedb
mauricedb / gist:5290642
Last active December 15, 2015 16:39
Unit testing a WebAPI controller using the HttpClient and server config
[TestMethod]
public void PutViaServer()
{
// Arrange
var config = new HttpConfiguration();
WebApiConfig.Register(config);
var server = new HttpServer(config);
var client = new HttpClient(server);
var book = new Book();
@mauricedb
mauricedb / gist:5356933
Last active August 16, 2021 16:54
Return JSON data in a camelCase format from an ASP.NET WebAPI controller.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
@mauricedb
mauricedb / gist:5562976
Created May 12, 2013 09:30
Unit test an ASP.NET ApiController that uses the ApiController.Request property
[TestMethod]
public void GetWithAnUnknownIdShouldReturnNotFound()
{
// Arrange
var controller = new BooksController()
{
Request = new HttpRequestMessage()
{
Properties = { { HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration() } }
}
@mauricedb
mauricedb / gist:5612901
Created May 20, 2013 15:20
Utility class for test enabling the System.Net.Http.HttpClient
public class TestingDelegatingHandler<T> : DelegatingHandler
{
private Func<HttpRequestMessage, HttpResponseMessage> _httpResponseMessageFunc;
public TestingDelegatingHandler(T value)
: this(HttpStatusCode.OK, value)
{ }
public TestingDelegatingHandler(HttpStatusCode statusCode)
@mauricedb
mauricedb / gist:11350476
Created April 27, 2014 17:04
Validating data using ClearScript/V8 and JavaScript in the WebAPI
using (var engine = new V8ScriptEngine())
{
var settings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
var jsonBook = JsonConvert.SerializeObject(newBook, settings);
engine.Execute("var book = " + jsonBook);
var path = HttpContext.Current.Server.MapPath(@"~\App\ValidateBook.js");
engine.Execute(File.ReadAllText(path));
var result = engine.Evaluate("validateBook(book)") as string;
@mauricedb
mauricedb / Script1.js
Last active August 29, 2015 14:02
X things every JavaScript developer should know: scoping - Wrong
var text = "Hello from script 1";
setTimeout(function () {
console.log(text);
}, 1000);
@mauricedb
mauricedb / Script1.js
Last active August 29, 2015 14:02
X things every JavaScript developer should know: scoping - Right
(function() {
var text = "Hello from script 1";
setTimeout(function () {
console.log(text);
}, 1000);
}());
@mauricedb
mauricedb / CodedUIWithPageObjectModel.cs
Created May 6, 2015 19:58
CodedUIWithPageObjectModel
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UITesting.HtmlControls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CodedUITestProject1
{
[CodedUITest]
public class CodedUIWithPageObjectModel
{
public class HomeController : Controller
{
private readonly IRuntimeEnvironment _runtimeEnvironment;
public HomeController(IRuntimeEnvironment runtimeEnvironment)
{
_runtimeEnvironment = runtimeEnvironment;
}
public IActionResult About()