Using Unity to Unit Test

Oct 1, 2009 at 7:56 PM

Hello I am new to unity and trying to understand more about how it can help with unit testing.  I realize its power but am trying to get it setup....

Enclosed is a unit test of the randomquoteservice that comes with the Hands on lab...  any help on getting this setup would be greatly appreciated

using

Microsoft.Practices.Unity;

using

StocksTicker.StockQuoteServices;

using

Microsoft.VisualStudio.TestTools.UnitTesting;

using

Microsoft.VisualStudio.TestTools.UnitTesting.Web;

using

System.Collections.Generic;

using

StocksTicker.Loggers;

namespace

{

 

 

 

StocksTickerTest/// <summary>

 

///This is a test class for RandomStockQuoteServiceTest and is intended

 

///to contain all RandomStockQuoteServiceTest Unit Tests

 

///</summary>

[

 

TestClass()]public class RandomStockQuoteServiceTest

{

 

 

 

 

 

 

private TestContext testContextInstance;private IUnityContainer unityContainer;private IStockQuoteService model;/// <summary>

 

///Gets or sets the test context which provides

 

///information about and functionality for the current test run.

 

///</summary>

 

{

 

public TestContext TestContextget

{

 

}

 

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

[

 

{

 

.RegisterType<

 

 

}

 

TestInitialize()]public void MyTestInitialize()IUnityContainer container = new UnityContainer()IStockQuoteService, RandomStockQuoteService>();this.unityContainer = container;this.model = this.unityContainer.Resolve<IStockQuoteService>(); //

 

//Use TestCleanup to run code after each test has run

 

//[TestCleanup()]

 

//public void MyTestCleanup()

 

//{

 

//}

 

//

#endregion

 

 

/// <summary>

 

///A test for Logger

 

///</summary>

 

// TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,

 

// http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,

 

// whether you are testing a page, web service, or a WCF service.

[

 

{

 

 

TestMethod()]public void LoggerTest()ILogger expected = null; // TODO: Initialize to an appropriate value

 

model.Logger = expected;

actual = target.Logger;

 

}

 

ILogger actual;Assert.AreEqual(expected, actual);/// <summary>

 

///A test for GenerateRandomQuote

 

///</summary>

 

// TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,

 

// http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,

 

// whether you are testing a page, web service, or a WCF service.

[

[

 

{

 

TestMethod()]DeploymentItem("StocksTicker.dll")]public void GenerateRandomQuoteTest()RandomStockQuoteService_Accessor target = new RandomStockQuoteService_Accessor(); // TODO: Initialize to an appropriate value

 

string symbol = "GE"; // TODO: Initialize to an appropriate value

 

StockQuote expected = null; // TODO: Initialize to an appropriate value

 

actual = target.GenerateRandomQuote(symbol);

 

}

 

StockQuote actual;Assert.AreEqual(expected, actual);/// <summary>

 

///A test for GetQuotes

 

///</summary>

 

// TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,

 

// http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,

 

// whether you are testing a page, web service, or a WCF service.

[

 

{

 

TestMethod()]public void GetQuotesTest()RandomStockQuoteService target = new RandomStockQuoteService(); // TODO: Initialize to an appropriate value

 

IEnumerable<string> symbols = null; // TODO: Initialize to an appropriate value

 

IDictionary<string, StockQuote> expected = null; // TODO: Initialize to an appropriate value

 

actual = target.GetQuotes(symbols);

 

 

}

}

}

IDictionary<string, StockQuote> actual;Assert.AreEqual(expected, actual);Assert.Inconclusive("Verify the correctness of this test method.");

Oct 3, 2009 at 4:50 AM

It's hard to tell - your code posting got seriously trashed. Could you post a smaller sample, without all the autogenerated comments?

 

 

Oct 5, 2009 at 1:44 PM

I wrote this symbol test to test for not null

[TestMethod()]
        public void GetQuotesTest_NotNull_Success()
        {
            IUnityContainer container = new UnityContainer();
            container.RegisterType<IStockQuoteService, RandomStockQuoteService>(new InjectionMember[] );
            container.RegisterType<ILogger,NullLogger>();
            var stockQuoteService = container.Resolve<IStockQuoteService>();
            var symbols = new List<string>();
            symbols.Add("GE");               
            IDictionary<string, StockQuote> actual = stockQuoteService.GetQuotes(symbols);
            Assert.IsNotNull(actual)
        }

I am looking for more on how to using unity for the build up with the tests.

 

Your help is appreciated.