Skip to content

Instantly share code, notes, and snippets.

@cromwellryan
Created April 11, 2012 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cromwellryan/2362133 to your computer and use it in GitHub Desktop.
Save cromwellryan/2362133 to your computer and use it in GitHub Desktop.
No Smiles Dynamic
using System;
using System.Dynamic;
using System.Linq;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
public class SmokeTestController : Controller
{
private readonly Func<SmokeTestResult>[] smokeTests;
public SmokeTestController(Func<SmokeTestResult>[] smokeTests)
{
this.smokeTests = smokeTests;
}
public ActionResult Index()
{
dynamic testResults = from test in smokeTests
let result = test()
select new
{
ClassName = result.GetType().Name.Replace("TestResult", "")
};
dynamic viewModel = new ExpandoObject();
viewModel.TestResults = testResults;
return View(viewModel);
}
}
public class FailureTestResult : SmokeTestResult
{
}
public class SmokeTestResult
{
}
}
using System;
using System.Web.Mvc;
using FluentAssertions;
using MvcApplication1.Controllers;
namespace MvcApplication1.Tests
{
using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
[TestFixture]
public class SmokeTestTests
{
[Test]
public void ShouldMakeMeSmile()
{
Func<SmokeTestResult> failingTest = () => new FailureTestResult();
var results = new SmokeTestController(new[] { failingTest }).Index().As<ViewResult>();
dynamic viewmodel = results.Model;
viewmodel.TestResults.First().Should().BeEquivalent("failure");
}
}
}
Test Name: ShouldMakeMeSmile
Test FullName: MvcApplication1.Tests.SmokeTestTests.ShouldMakeMeSmile
Test Source: c:\Users\ryan.cromwell\Documents\Visual Studio 11\Projects\MvcApplication1\MvcApplication1\Tests\SmokeTestTests.cs : line 16
Test Outcome: Failed
Test Duration: 0:00:00.5489077
Result Message:
Test method MvcApplication1.Tests.SmokeTestTests.ShouldMakeMeSmile threw exception:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'First'
Result StackTrace:
at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at MvcApplication1.Tests.SmokeTestTests.ShouldMakeMeSmile() in c:\Users\ryan.cromwell\Documents\Visual Studio 11\Projects\MvcApplication1\MvcApplication1\Tests\SmokeTestTests.cs:line 23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment