Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

looking at how to test drive the javascript portions of development, …

…found a way to integrate javascript unit tests with the rest of my test (NUnit, but really any that Studio supported).
  • Loading branch information...
commit 5880185924149267983f4287703a9f0e05c8ee28 1 parent fe0360b
Cory Nabours authored
View
18 Local.testsettings
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<TestSettings name="Local" id="6c82a647-3b75-468c-a1f3-cbe3e0ef8d2b" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
+ <Description>These are default test settings for a local test run.</Description>
+ <Deployment>
+ <DeploymentItem filename="NaboursCRM.UI.Javascript.Tests\Scripts\" />
+ </Deployment>
+ <Execution>
+ <TestTypeSpecific>
+ <UnitTestRunConfig testTypeId="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b">
+ <AssemblyResolution>
+ <TestDirectory useLoadContext="true" />
+ </AssemblyResolution>
+ </UnitTestRunConfig>
+ </TestTypeSpecific>
+ <AgentRule name="Execution Agents">
+ </AgentRule>
+ </Execution>
+</TestSettings>
View
76 NaboursCRM.UI.Javascript.Tests/JavascriptTestHelper.cs
@@ -0,0 +1,76 @@
+using System;
+using System.IO;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using MSScriptControl;
+
+namespace NaboursCRM.UI.Javascript.Tests
+{
+ public class JavascriptTestHelper : IDisposable
+ {
+ private ScriptControl _sc;
+ private TestContext _context;
+ /// <summary>
+ /// You need to use this helper with Unit Tests and not
+ /// Basic Unit Tests because you need a Test Context
+ /// </summary>
+ /// <param name="testContext">Unit Test Test Context</param>
+ public JavascriptTestHelper(TestContext testContext)
+ {
+ if (testContext == null)
+ {
+ throw new ArgumentNullException("TestContext");
+ }
+ _context = testContext;
+ _sc = new ScriptControl();
+ _sc.Language = "JScript";
+ _sc.AllowUI = false;
+ }
+
+ /// <summary>
+ /// Load the contents of a JavaScript file into the
+ /// Script Engine.
+ /// </summary>
+ /// <param name="path">Path to JavaScript file</param>
+ public void LoadFile(string path)
+ {
+ var fileContents = File.ReadAllText(path);
+ _sc.AddCode(fileContents);
+ }
+
+ /// <summary>
+ /// Pass the path of the test that you want to execute.
+ /// </summary>
+ /// <param name="testMethodName">JavaScript function name</param>
+ public void ExecuteTest(string testMethodName)
+ {
+ dynamic result = null;
+ try
+ {
+ result = _sc.Run(testMethodName, new object[] {});
+ }
+ catch
+ {
+ var error = ((IScriptControl) _sc).Error;
+ if (error != null)
+ {
+ var description = error.Description;
+ var line = error.Line;
+ var column = error.Column;
+ var text = error.Text;
+ var source = error.Source;
+ if (_context != null)
+ {
+ var details = String.Format("{0} \r\nLine: {1} Column: {2}", source, line, column);
+ _context.WriteLine(details);
+ }
+ }
+ throw new AssertFailedException(error.Description);
+ }
+ }
+
+ public void Dispose()
+ {
+ _sc = null;
+ }
+ }
+}
View
76 NaboursCRM.UI.Javascript.Tests/NaboursCRM.UI.Javascript.Tests.csproj
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>
+ </ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{9F46B516-862A-4F86-9A36-72B1E06B5E48}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>NaboursCRM.UI.Javascript.Tests</RootNamespace>
+ <AssemblyName>NaboursCRM.UI.Javascript.Tests</AssemblyName>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
+ <Reference Include="System" />
+ <Reference Include="System.Core">
+ <RequiredTargetFramework>3.5</RequiredTargetFramework>
+ </Reference>
+ </ItemGroup>
+ <ItemGroup>
+ <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+ <Visible>False</Visible>
+ </CodeAnalysisDependentAssemblyPaths>
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="JavascriptTestHelper.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="UnitTest1.cs" />
+ <Compile Include="SampleMathTest.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <COMReference Include="MSScriptControl">
+ <Guid>{0E59F1D2-1FBE-11D0-8FF2-00A0D10038BC}</Guid>
+ <VersionMajor>1</VersionMajor>
+ <VersionMinor>0</VersionMinor>
+ <Lcid>0</Lcid>
+ <WrapperTool>tlbimp</WrapperTool>
+ <Isolated>False</Isolated>
+ <EmbedInteropTypes>True</EmbedInteropTypes>
+ </COMReference>
+ </ItemGroup>
+ <ItemGroup>
+ <Content Include="Scripts\JavaScriptUnitTestFramework.js" />
+ <Content Include="Scripts\MathTest.js" />
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
View
35 NaboursCRM.UI.Javascript.Tests/Properties/AssemblyInfo.cs
@@ -0,0 +1,35 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("NaboursCRM.UI.Javascript.Tests")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("Microsoft")]
+[assembly: AssemblyProduct("NaboursCRM.UI.Javascript.Tests")]
+[assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("dee0b165-2669-4c62-aabe-23201f5b126d")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
View
76 NaboursCRM.UI.Javascript.Tests/SampleMathTest.cs
@@ -0,0 +1,76 @@
+using System;
+using System.Text;
+using System.Collections.Generic;
+using System.Linq;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace NaboursCRM.UI.Javascript.Tests
+{
+ /// <summary>
+ /// Summary description for UnitTest2
+ /// </summary>
+ [TestClass]
+ public class SampleMathTest
+ {
+ public SampleMathTest()
+ {
+ //
+ // TODO: Add constructor logic here
+ //
+ }
+
+ private TestContext testContextInstance;
+
+ /// <summary>
+ ///Gets or sets the test context which provides
+ ///information about and functionality for the current test run.
+ ///</summary>
+ public TestContext TestContext
+ {
+ get
+ {
+ return testContextInstance;
+ }
+ set
+ {
+ testContextInstance = value;
+ }
+ }
+
+ #region Additional test attributes
+ //
+ // You can use the following additional attributes as you write your tests:
+ //
+ // Use ClassInitialize to run code before running the first test in the class
+ // [ClassInitialize()]
+ // public static void MyClassInitialize(TestContext testContext) { }
+ //
+ // Use ClassCleanup to run code after all tests in a class have run
+ // [ClassCleanup()]
+ // public static void MyClassCleanup() { }
+ //
+ // Use TestInitialize to run code before running each test
+ // [TestInitialize()]
+ // public void MyTestInitialize() { }
+ //
+ // Use TestCleanup to run code after each test has run
+ // [TestCleanup()]
+ // public void MyTestCleanup() { }
+ //
+ #endregion
+
+ [TestMethod]
+ public void TestAddNumbers()
+ {
+ var jsHelper = new JavascriptTestHelper(this.TestContext);
+
+ //Load JavaScript files.
+ jsHelper.LoadFile("JavaScriptUnitTestFramework.js");
+ jsHelper.LoadFile(@"..\..\..\..\..\..\NaboursCRM\Scripts\Math.js");
+ jsHelper.LoadFile("MathTest.js");
+
+ //Execute JavaScript test
+ jsHelper.ExecuteTest("testAddNumbers");
+ }
+ }
+}
View
7 NaboursCRM.UI.Javascript.Tests/Scripts/JavaScriptUnitTestFramework.js
@@ -0,0 +1,7 @@
+var assert = {
+ areEqual: function (expected, actual, message) {
+ if (expected !== actual) {
+ throw new Error("Expected value " + expected + " is not equal to " + actual + ". " + message);
+ }
+ }
+};
View
7 NaboursCRM.UI.Javascript.Tests/Scripts/MathTest.js
@@ -0,0 +1,7 @@
+/// <reference path="JavaScriptUnitTestFramework.js"/>
+function testAddNumbers() {
+ // Act
+ var result = addNumbers(1, 3);
+ // Assert
+ assert.areEqual(4, result, "addNumbers did not return right value!");
+}
View
16 NaboursCRM.sln
@@ -15,7 +15,19 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NaboursCRM.Service", "Nabou
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NaboursCRM.Service.Tests", "NaboursCRM.Service.Tests\NaboursCRM.Service.Tests.csproj", "{FE701411-D547-4DCE-854D-A7A836D410DA}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NaboursCRM.UI.Javascript.Tests", "NaboursCRM.UI.Javascript.Tests\NaboursCRM.UI.Javascript.Tests.csproj", "{9F46B516-862A-4F86-9A36-72B1E06B5E48}"
+EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{833248DB-A239-47A8-85DB-FF10F35E29D5}"
+ ProjectSection(SolutionItems) = preProject
+ Local.testsettings = Local.testsettings
+ NaboursCRM.vsmdi = NaboursCRM.vsmdi
+ TraceAndTestImpact.testsettings = TraceAndTestImpact.testsettings
+ EndProjectSection
+EndProject
Global
+ GlobalSection(TestCaseManagementSettings) = postSolution
+ CategoryFile = NaboursCRM.vsmdi
+ EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
@@ -49,6 +61,10 @@ Global
{FE701411-D547-4DCE-854D-A7A836D410DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FE701411-D547-4DCE-854D-A7A836D410DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FE701411-D547-4DCE-854D-A7A836D410DA}.Release|Any CPU.Build.0 = Release|Any CPU
+ {9F46B516-862A-4F86-9A36-72B1E06B5E48}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {9F46B516-862A-4F86-9A36-72B1E06B5E48}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {9F46B516-862A-4F86-9A36-72B1E06B5E48}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {9F46B516-862A-4F86-9A36-72B1E06B5E48}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
View
6 NaboursCRM.vsmdi
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<TestLists xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
+ <TestList name="Lists of Tests" id="8c43106b-9dc1-4907-a29f-aa66a61bf5b6">
+ <RunConfiguration id="6c82a647-3b75-468c-a1f3-cbe3e0ef8d2b" name="Local" storage="local.testsettings" type="Microsoft.VisualStudio.TestTools.Common.TestRunConfiguration, Microsoft.VisualStudio.QualityTools.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
+ </TestList>
+</TestLists>
View
12 NaboursCRM/NaboursCRM.UI.csproj
@@ -64,6 +64,7 @@
</ItemGroup>
<ItemGroup>
<Content Include="Contacts.svc" />
+ <Content Include="Scripts\Math.js" />
<Content Include="Styles\Site.css" />
<Content Include="Default.aspx" />
<Content Include="Global.asax" />
@@ -110,6 +111,17 @@
<Name>NaboursCRM.Service</Name>
</ProjectReference>
</ItemGroup>
+ <ItemGroup>
+ <COMReference Include="MSScriptControl">
+ <Guid>{0E59F1D2-1FBE-11D0-8FF2-00A0D10038BC}</Guid>
+ <VersionMajor>1</VersionMajor>
+ <VersionMinor>0</VersionMinor>
+ <Lcid>0</Lcid>
+ <WrapperTool>tlbimp</WrapperTool>
+ <Isolated>False</Isolated>
+ <EmbedInteropTypes>True</EmbedInteropTypes>
+ </COMReference>
+ </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
<ProjectExtensions>
View
3  NaboursCRM/Scripts/Math.js
@@ -0,0 +1,3 @@
+function addNumbers(a, b) {
+ return 5;
+}
View
9 TraceAndTestImpact.testsettings
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<TestSettings name="Trace and Test Impact" id="692b7356-a661-48f0-ad10-856ed9bfe005" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
+ <Description>These are test settings for Trace and Test Impact.</Description>
+ <Execution>
+ <TestTypeSpecific />
+ <AgentRule name="Execution Agents">
+ </AgentRule>
+ </Execution>
+</TestSettings>
Please sign in to comment.
Something went wrong with that request. Please try again.