Skip to content

Instantly share code, notes, and snippets.

@scottlaw1
scottlaw1 / ValueObjectTest.cs
Created February 24, 2015 16:20
XUnit test for interface implementation
public class ValueObjectTest
{
[Fact]
public void AllValueObjectsImplementIEquatable()
{
var typeToFind = typeof (IValueObject);
var typesFound = AppDomain.CurrentDomain.GetAssemblies().ToList()
.SelectMany(s => s.GetTypes())
.Where(typeToFind.IsAssignableFrom);
@scottlaw1
scottlaw1 / gist:ca7996c4ce97453fa9d5
Created January 2, 2015 20:30
Bad Form Submit vs Not Bad Form Submit
<!--BAD-->
<!-- The following button tag will submit the form named "formName" TWICE -->
<button type="submit" onclick="$('#formName').submit()">Assign</button>
<!--Not Bad-->
<!-- The following button tag will submit the form named "formName" ONCE -->
<button type="button" onclick="$('#formName').submit()">Assign</button>
@scottlaw1
scottlaw1 / UnitTestBase.cs
Created June 29, 2014 15:11
Example of abstract class I use for unit testing with XUnit
public abstract class UnitTestBase : IUseFixture<UnitTestFixture>{
public void SetFixture(UnitTestFixture data){
Fixture = data;
AdditionalFixtureConfiguration();
}
protected UnitTestFixture Fixture {get;set;}
public virtual void AdditionalFixtureConfiguration(){
@scottlaw1
scottlaw1 / LINQPad Code Generation Example
Created March 19, 2014 21:08
An example of a program written in LINQPad for code generation of classes used in an ASP.NET MVC project.
void Main()
{
string aclTemplates = @"
using System;
using Acme.Web.Areas.Area1.Controllers.{0};
using Acme.Web.Infrastructure.Acl;
namespace Ltss.Web.Areas.Acme.Acl.{0}
{{
public class Acme{0}AclRegistry : AbstractAccessControlRegistry<{0}Controller>
@scottlaw1
scottlaw1 / gist:5960394
Created July 9, 2013 19:20
XUnit test class for finding derived types that are missing an explicit public no-arg constructor
public class SomeTypeTest
{
/// <summary>
/// <remarks>A bit of a hack, but if this test fails, the last type name written to the console/debug output
/// is the one missing an explicit public no-arg constructor.</remarks>
/// </summary>
[Fact]
public void AllTypesDerivedFromSomeTypeHavePublicNoArgConstructors()
{
var type = typeof(Enumeration);
@scottlaw1
scottlaw1 / AutoMapperTest.cs
Created January 17, 2013 23:05
Revision to an XUnit test for identifying bad mappings. Unlike the standard test (which would fail after the first bad/missing mapping), this will print out all the unmapped property names in the solution in context with the mapping they belong to.
[Fact]
public void AssertConfigurationIsValid()
{
try
{
Mapper.AssertConfigurationIsValid();
}
catch (AutoMapperConfigurationException amce)
{
foreach (var e in amce.Errors)
@scottlaw1
scottlaw1 / MissingFileTest.cs
Created November 23, 2012 14:39
XUnit test for missing files
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Xunit;
namespace Isas.Shared.Tests
{
@scottlaw1
scottlaw1 / gist:3089962
Created July 11, 2012 12:06
AppleScript + RSVP Emails = Wedding Guests Address Book Group
tell application "Mail"
set selectedMessages to selection
if (count of selectedMessages) is equal to 0 then
display alert "No Messages Selected" message "Select the messages you want to add to 'wedding guests' address book group before running this script."
else
set weddingGuests to {}
repeat with eachMessage in selectedMessages
set emailSender to (extract name from sender of eachMessage) as string
@scottlaw1
scottlaw1 / gist:3089941
Created July 11, 2012 12:03
Create comma-delimited list of values with LINQ aggregate
int[] numbers = new[] {1, 5, 8, 26, 35, 42};
var result = numbers.Aggregate("", (current, item) => current + item.ToString() + ",");
@scottlaw1
scottlaw1 / gist:3089921
Created July 11, 2012 11:55
Inserting stored procedure results into a table variable
CREATE PROC test_proc
AS
SET NOCOUNT ON
SELECT TOP 10 id, name FROM employee
declare @sometable as table([id] int, name varchar(64))
INSERT INTO @sometable
exec test_proc