<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx</link><description>In my previous post about unit testing WCF services , I hinted at the need to perform integration testing of services as well. As Jimmy writes , you should still place your logic involving OperationContext in the Service Interface Layer (SIL): In many</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>ploeh blog : Unit Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#1207334</link><pubDate>Mon, 04 Dec 2006 22:45:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1207334</guid><dc:creator>ploeh blog : Unit Testing WCF Services</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/ploeh/archive/2006/12/03/UnitTestingWCFServices.aspx"&gt;http://blogs.msdn.com/ploeh/archive/2006/12/03/UnitTestingWCFServices.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#1207628</link><pubDate>Tue, 05 Dec 2006 00:14:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1207628</guid><dc:creator>jimmytr</dc:creator><description>&lt;p&gt;For in-the-same-project-kinda-tests I often use the ChannelFactory generic which in its simplest form could be used in your example as:&lt;/p&gt;
&lt;p&gt;Binding b = new WSHttpBinding();&lt;/p&gt;
&lt;p&gt;ChannelFactory&amp;lt;IMyService&amp;gt; fact = &lt;/p&gt;
&lt;p&gt;new ChannelFactory&amp;lt;IMyService&amp;gt;(b,&amp;quot;&lt;a rel="nofollow" target="_new" href="http://localhost:8080/myservice&amp;quot;"&gt;http://localhost:8080/myservice&amp;quot;&lt;/a&gt;);&lt;/p&gt;
&lt;p&gt;IMyService isrv = fact.CreateChannel();&lt;/p&gt;
&lt;p&gt;string a = isrv.DoStuff(&amp;quot;test&amp;quot;);&lt;/p&gt;
&lt;p&gt;Which will of course only work if the config file in your example specify a http binding with the address above.&lt;/p&gt;
&lt;p&gt;Good post! Look forward to reading the next one.&lt;/p&gt;
</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#1207659</link><pubDate>Tue, 05 Dec 2006 00:22:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1207659</guid><dc:creator>jimmytr</dc:creator><description>&lt;p&gt;I forgot to mention that the programmatic approach I showed above has the benefit that you don't need to expose a metadata service endpoint that tools like svcutil.exe will be looking for.&lt;/p&gt;
</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#1210345</link><pubDate>Tue, 05 Dec 2006 12:28:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1210345</guid><dc:creator>ploeh</dc:creator><description>&lt;p&gt;Hi Jimmy&lt;/p&gt;
&lt;p&gt;Thank you for your comments - good points, both, although whether svcutil will need a metadata endpoint depends on whether you generate a proxy from a live service, or a wsdl file. A metadata endpoint isn't required in the latter case :)&lt;/p&gt;
&lt;p&gt;But I think I'll try out your approach the next time around :)&lt;/p&gt;
</description></item><item><title>Integration Testing With Certificates</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#1333577</link><pubDate>Thu, 21 Dec 2006 00:13:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1333577</guid><dc:creator>ploeh blog</dc:creator><description>&lt;p&gt;In my post about integration testing of WCF services , I hinted that one compelling reason to perform&lt;/p&gt;
</description></item><item><title>Mapping SAML Tokens to IPrincipals</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#1412088</link><pubDate>Thu, 04 Jan 2007 21:49:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1412088</guid><dc:creator>ploeh blog</dc:creator><description>&lt;p&gt;In my post about integration testing of WCF services , I briefly touched on the topic of authorization&lt;/p&gt;
</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#1616529</link><pubDate>Wed, 07 Feb 2007 08:17:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1616529</guid><dc:creator>RajeshPR</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;Great article , was looking for some help on using NUnit with WCF.&lt;/p&gt;
&lt;p&gt;I will just briefly explain my scenario of what WCF is doing and then put across my queries related to NUint testing.&lt;/p&gt;
&lt;p&gt;My WCF Service does the basic work of performing DB CRUD operation say for example in the code snippet below it does the work of fetching list of employees and adding/updating new employee.&lt;/p&gt;
&lt;p&gt;The service and operation contract are defined as below&lt;/p&gt;
&lt;p&gt;[ServiceContract()]&lt;/p&gt;
&lt;p&gt;public interface IMyService&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;[OperationContract]&lt;/p&gt;
&lt;p&gt;EmpStructure[] GetEmployees();&lt;/p&gt;
&lt;p&gt;[OperationContract]&lt;/p&gt;
&lt;p&gt;string AddEmployee(EmpStructure dataContractValue);&lt;/p&gt;
&lt;p&gt;[OperationContract]&lt;/p&gt;
&lt;p&gt;string UpdateEmployee(EmpStructure dataContractValue);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;My WCF Service class is defined as below and it implements the operation contract GetEmployees(), which returns a list of employees. This is for select operation&lt;/p&gt;
&lt;p&gt;public class MyService : IMyService&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;public EmpStructure[] GetEmployees() &lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;EmpList response = new EmpList();&lt;/p&gt;
&lt;p&gt;ArrayList emps = new ArrayList();&lt;/p&gt;
&lt;p&gt;int i = 0;&lt;/p&gt;
&lt;p&gt;string EmployeeName=&amp;quot;&amp;quot;;&lt;/p&gt;
&lt;p&gt;SqlConnection cnn = new SqlConnection(ConfigurationSettings.AppSettings[&amp;quot;ConnectionString&amp;quot;]);&lt;/p&gt;
&lt;p&gt;SqlCommand cmd = new SqlCommand(&amp;quot;SELECT * FROM emp&amp;quot; , cnn);&lt;/p&gt;
&lt;p&gt;cnn.Open();&lt;/p&gt;
&lt;p&gt;SqlDataReader reader = cmd.ExecuteReader();&lt;/p&gt;
&lt;p&gt;while (reader.Read())&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;EmpStructure Emp = new EmpStructure();&lt;/p&gt;
&lt;p&gt;Emp.EmpId =Convert.ToInt32(reader[&amp;quot;EmpId&amp;quot;]);&lt;/p&gt;
&lt;p&gt;Emp.EmpName =(string)reader[&amp;quot;Empname&amp;quot;];&lt;/p&gt;
&lt;p&gt;Emp.Basic =(decimal)reader[&amp;quot;Basic&amp;quot;];&lt;/p&gt;
&lt;p&gt;emps.Add(Emp);&lt;/p&gt;
&lt;p&gt;i++;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;reader.Close();&lt;/p&gt;
&lt;p&gt;cnn.Close();&lt;/p&gt;
&lt;p&gt;response.Employees = new EmpStructure;&lt;/p&gt;
&lt;p&gt;i=0;&lt;/p&gt;
&lt;p&gt;foreach (EmpStructure Employee in emps)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;response.Employees = Employee;&lt;/p&gt;
&lt;p&gt;i++;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;return response.Employees ;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;This operation defines Adding of a new employee&lt;/p&gt;
&lt;p&gt;public string AddEmployee(EmpStructure dataContractValue)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;SqlConnection cnn = new SqlConnection(ConfigurationSettings.AppSettings[&amp;quot;ConnectionString&amp;quot;]);&lt;/p&gt;
&lt;p&gt;SqlCommand cmd = new SqlCommand(&amp;quot;AddEmp&amp;quot;, cnn);&lt;/p&gt;
&lt;p&gt;cmd.CommandType = CommandType.StoredProcedure;&lt;/p&gt;
&lt;p&gt;cmd.Parameters.Add(new SqlParameter(&amp;quot;@EmpName&amp;quot;, SqlDbType.NVarChar, 50));&lt;/p&gt;
&lt;p&gt;cmd.Parameters.Add(new SqlParameter(&amp;quot;@Basic&amp;quot;, SqlDbType.Decimal));&lt;/p&gt;
&lt;p&gt;cmd.Parameters[&amp;quot;@EmpName&amp;quot;].Value =dataContractValue.EmpName;&lt;/p&gt;
&lt;p&gt;cmd.Parameters[&amp;quot;@Basic&amp;quot;].Value = dataContractValue.Basic;&lt;/p&gt;
&lt;p&gt;cnn.Open();&lt;/p&gt;
&lt;p&gt;cmd.ExecuteNonQuery();&lt;/p&gt;
&lt;p&gt;cnn.Close();&lt;/p&gt;
&lt;p&gt;return &amp;quot;Added Successfully&amp;quot; ;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;This operation defines updating an employee information&lt;/p&gt;
&lt;p&gt;public string UpdateEmployee(EmpStructure dataContractValue)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;SqlConnection cnn = new SqlConnection(ConfigurationSettings.AppSettings[&amp;quot;ConnectionString&amp;quot;]);&lt;/p&gt;
&lt;p&gt;SqlCommand cmd = new SqlCommand(&amp;quot;UpdateEmp&amp;quot;, cnn);&lt;/p&gt;
&lt;p&gt;cmd.CommandType = CommandType.StoredProcedure;&lt;/p&gt;
&lt;p&gt;cmd.Parameters.Add(new SqlParameter(&amp;quot;@EmpName&amp;quot;, SqlDbType.NVarChar, 50));&lt;/p&gt;
&lt;p&gt;cmd.Parameters.Add(new SqlParameter(&amp;quot;@Basic&amp;quot;, SqlDbType.Decimal));&lt;/p&gt;
&lt;p&gt;cmd.Parameters.Add(new SqlParameter(&amp;quot;@EmpID&amp;quot;, SqlDbType.Int ));&lt;/p&gt;
&lt;p&gt;cmd.Parameters[&amp;quot;@EmpName&amp;quot;].Value =dataContractValue.EmpName;&lt;/p&gt;
&lt;p&gt;cmd.Parameters[&amp;quot;@Basic&amp;quot;].Value = dataContractValue.Basic;&lt;/p&gt;
&lt;p&gt;cmd.Parameters[&amp;quot;@EmpId&amp;quot;].Value = dataContractValue.EmpId ; &lt;/p&gt;
&lt;p&gt;cnn.Open();&lt;/p&gt;
&lt;p&gt;cmd.ExecuteNonQuery();&lt;/p&gt;
&lt;p&gt;cnn.Close();&lt;/p&gt;
&lt;p&gt;return &amp;quot;Updated Successfully&amp;quot; ;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;I also have an internal class which is basically used to enummerate through the list of employees and returns a collection of employee. This is represented using the EmpList class&lt;/p&gt;
&lt;p&gt;[Serializable]&lt;/p&gt;
&lt;p&gt;public class EmpList&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;private EmpStructure [] employees;&lt;/p&gt;
&lt;p&gt;public EmpStructure [] Employees&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;get { return employees; }&lt;/p&gt;
&lt;p&gt;set { employees = value; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Next I have a DataContract called EmpStructure which represents the values to be passed back n forth the service and which is actually a object representation for the Employee Table.&lt;/p&gt;
&lt;p&gt;[DataContract]&lt;/p&gt;
&lt;p&gt;public class EmpStructure&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;int empId;&lt;/p&gt;
&lt;p&gt;string empName;&lt;/p&gt;
&lt;p&gt;decimal basic; &lt;/p&gt;
&lt;p&gt;[DataMember]&lt;/p&gt;
&lt;p&gt;public int EmpId&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;get { return empId;}&lt;/p&gt;
&lt;p&gt;set { empId = value;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;[DataMember]&lt;/p&gt;
&lt;p&gt;public string EmpName&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;get { return empName;}&lt;/p&gt;
&lt;p&gt;set { empName = value;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;[DataMember]&lt;/p&gt;
&lt;p&gt;public decimal Basic&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;get { return basic;}&lt;/p&gt;
&lt;p&gt;set { basic = value;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Questions:&lt;/p&gt;
&lt;p&gt;1. How do I perform unit tests for the above DB related operations in WCF using NUint.&lt;/p&gt;
&lt;p&gt;2. Even if i decouple my service layer and data operation layer (into an class), how would I perform NUnit tests on the class.&lt;/p&gt;
&lt;p&gt;3. I tried creating a NUnit test scenario for the above service proxy, it looks like this &lt;/p&gt;
&lt;p&gt;code snippet:&lt;/p&gt;
&lt;p&gt;[TestFixture]&lt;/p&gt;
&lt;p&gt;public class TestClass&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;WCFTest.MyServiceClient testClient;&lt;/p&gt;
&lt;p&gt;WCFTest.EmpStructure[] empObj;&lt;/p&gt;
&lt;p&gt;[SetUp]&lt;/p&gt;
&lt;p&gt;public void Init()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;testClient = new WCFTestClasses.WCFTest.MyServiceClient();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;[Test]&lt;/p&gt;
&lt;p&gt;public void getEmp()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;empObj = testClient.GetString();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;4. In the above case how will I perform my success and failure tests using Asserts....&lt;/p&gt;
&lt;p&gt;5. Is there any sample available which shows how to perform unit tests for CRUD operations using NUint.&lt;/p&gt;
</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#1618278</link><pubDate>Wed, 07 Feb 2007 12:47:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1618278</guid><dc:creator>ploeh</dc:creator><description>&lt;P&gt;Hi Rajesh&lt;/P&gt;
&lt;P&gt;Thank you for writing. I think I can answer most of your questions, but before I go ahead and do that, we need to establish some common ground:&lt;/P&gt;
&lt;P&gt;What you are attempting to perform are &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2006/08/27/727211.aspx"&gt;not unit tests but integration tests&lt;/A&gt;. That doesn't preclude the use of unit testing tools, but personally, I find the distinction important.&lt;/P&gt;
&lt;P&gt;Another thing is that most of my code samples and posts relate to the use of Visual Studio Team System unit tests, but you should be aware that all the techniques and principles I outline, apply equally well to NUnit, with only slight modifications.&lt;/P&gt;
&lt;P&gt;With that out of the way, I'll attempt to answer your questions:&lt;/P&gt;
&lt;P&gt;1. There are two schools for integration testing of databases. In my post about &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2006/11/17/DataAccessComponentTesting.aspx"&gt;Data Access Component Testing&lt;/A&gt;, I outline how I prefer to do it. In the MSDN Magazine article &lt;A class="" href="http://msdn.microsoft.com/msdnmag/issues/05/06/UnitTesting/default.aspx"&gt;Know Thy Code: Simplify Data Layer Unit Testing using Enterprise Services&lt;/A&gt;, you can read about a different approach to data access testing.&lt;/P&gt;
&lt;P&gt;2. The normal way to decouple data access is to hide the data access component behind an interface or base class. You can then use some form of &lt;A class="" href="http://www.martinfowler.com/articles/injection.html"&gt;dependency injection&lt;/A&gt; to populate the service layer with an instance that implements the relevant interface. In a unit test, you would then create a stub or mock of that interface and inject that into your service class, thus completely removing the database from the equation.&lt;/P&gt;
&lt;P&gt;3 &amp;amp; 4. In this case, I don't understand exactly what your MyServiceClient class does. Where does the GetString method come from? That operation is not part of the service contract, as far as I can see.&lt;/P&gt;
&lt;P&gt;If you want to validate, say, the GetEmployees method, you would need to call the GetEmployees method on the proxy and write the necessary Asserts against the return value.&lt;/P&gt;
&lt;P&gt;5. Please see my post about &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2006/11/17/DataAccessComponentTesting.aspx"&gt;Data Access Component Testing&lt;/A&gt; for an outline of the principles.&lt;/P&gt;
&lt;P&gt;HTH&lt;/P&gt;</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#1864201</link><pubDate>Mon, 12 Mar 2007 15:18:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1864201</guid><dc:creator>Ravi</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;How to use context in Nunit? With MS unit test framework the function with ClassInitialize attribute have TestContext parameter but in NUnit how context can be used?&lt;/p&gt;
&lt;p&gt;Thanks in advance.&lt;/p&gt;
&lt;p&gt;Ravi&lt;/p&gt;</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#1864299</link><pubDate>Mon, 12 Mar 2007 15:32:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1864299</guid><dc:creator>ploeh</dc:creator><description>&lt;p&gt;Hi Ravi&lt;/p&gt;
&lt;p&gt;Thank you for your question. It's been a couple of years since I used NUnit, but as far as I remember, NUnit doesn't have a test context. To validate, you can ask on an NUnit forum.&lt;/p&gt;
&lt;p&gt;For a lot of tests, the test context is never used (as is the case in this article). In VSTS, the test context can be used e.g. for data-driven tests, but I don't think NUnit has this concept as well, which is probably the reason why no test context exists - at least, I never had any use for it when I used NUnit, and I still don't need it today with VSTS.&lt;/p&gt;
</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#1864466</link><pubDate>Mon, 12 Mar 2007 15:56:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1864466</guid><dc:creator>Ravi</dc:creator><description>&lt;p&gt;Thanks for your prompt reply :)&lt;/p&gt;
&lt;p&gt;I need to test methods of WCF service with NUnit.&lt;/p&gt;
&lt;p&gt;Actually, I have WCF service which contains use of OperationContext.Current from System.ServiceModel to retrieve some data like username. So how can pass the values to set OperationContext.Current from NUnit test method?&lt;/p&gt;</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#1864687</link><pubDate>Mon, 12 Mar 2007 16:17:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1864687</guid><dc:creator>ploeh</dc:creator><description>&lt;p&gt;Hi Ravi&lt;/p&gt;
&lt;p&gt;Thank you for clarifying your question. First of all: System.ServiceModel.OperationContext has nothing to do with Microsoft.VisualStudio.TestTools.UnitTesting.TestContext. In your case, that's only a good thing, since NUnit's lack of a test context doesn't stop you from testing services that use OperationContext.Current.&lt;/p&gt;
&lt;p&gt;It's pretty difficult to override OperationContext.Current from a unit test, but in theory, it should be possible, since this property is a static read/write property. This means that you can create a new instance of OperationContext in the beginning of your test, and then assign it to OperationContext.Current.&lt;/p&gt;
&lt;p&gt;It's not particularly simple to do, though, since OperationContext is a sealed class, so you can't stub it out. On the other hand, its single public constructor takes an instance of IContextChannel, so if you can stub that out, you should be good to go. However, this is quite a complext interface, so I've never really found it worthwhile to stub it out, but depending on your need, you might be able to get by that fairly easily be using a dynamic mock library.&lt;/p&gt;
&lt;p&gt;In any case, stubbing out OperationContext.Current is so difficult that I've personally never found it worth the while. That's why I, when I need to test code that accesses OperationContext.Current, find it far easier to write an integration test as outlined in this post. If you want to do it with NUnit, all you need to do is to change the signature of the InitializeClass and CleanupClass methods to fit NUnit's syntax.&lt;/p&gt;
&lt;p&gt;When you write an integration test as outlined here, WCF will automatically create and populate OperationContext.Current for you.&lt;/p&gt;
&lt;p&gt;HTH&lt;/p&gt;
</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#1872147</link><pubDate>Tue, 13 Mar 2007 14:48:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1872147</guid><dc:creator>Ravi</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Thanks again,&lt;/p&gt;
&lt;p&gt;Yes, it is difficult to set OperationContext.Current from within the nunit method since we dont have available OperationContext.Current.&lt;/p&gt;
&lt;p&gt;As u responded that if I write an integration test as outlined here... I wrote intergration test to open service host and it worked fine but OperationContext.Current doesnt get populate.&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#1872194</link><pubDate>Tue, 13 Mar 2007 14:59:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1872194</guid><dc:creator>ploeh</dc:creator><description>&lt;p&gt;Hi Ravi&lt;/p&gt;
&lt;p&gt;OperationContext.Current should be automatically created by the WCF pipeline before your service method is invoked. On the other hand, it will only be available in the service operations, and not when you, for instance, initialize your service class.&lt;/p&gt;
&lt;p&gt;I'm not sure if I understand correctly what you are doing. Maybe you could share a simple repro?&lt;/p&gt;
</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#1872970</link><pubDate>Tue, 13 Mar 2007 16:54:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1872970</guid><dc:creator>Ravi</dc:creator><description>&lt;p&gt;I have unit test class library project from where I used a method with[TestFixtureSetUp] attribute to initialize and run the service. In test method I called the service method which has OperationContext.Current to get username. Here I get null value for OperationContext.Current.&lt;/p&gt;</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#1873978</link><pubDate>Tue, 13 Mar 2007 19:26:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1873978</guid><dc:creator>ploeh</dc:creator><description>&lt;p&gt;Can you share a simple repro of this behavior? I can't readily reproduce it.&lt;/p&gt;
</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#1878420</link><pubDate>Wed, 14 Mar 2007 08:38:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1878420</guid><dc:creator>Ravi</dc:creator><description>&lt;p&gt;Here are unit test methods.&lt;/p&gt;
&lt;p&gt;[TestFixtureSetUp]&lt;/p&gt;
&lt;p&gt;public void MyClassInitialize()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;try&lt;/p&gt;
&lt;p&gt; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; Console.WriteLine(&amp;quot;Class initialized&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; serviceHost = new ServiceHost(typeof(MyService));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; serviceHost.AddServiceEndpoint(typeof(IMyService), new WSHttpBinding(),&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;quot;&lt;a rel="nofollow" target="_new" href="http://localhost/MyFirstService/MyService&amp;quot;"&gt;http://localhost/MyFirstService/MyService&amp;quot;&lt;/a&gt;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;serviceHost.Open();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;catch (Exception ex)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(ex.Message);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Assert.Fail(ex.Message);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt;	[TestMethod]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void GetUserDetails()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;try&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;Start calling GetUserDetails function&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; MyService myService = new MyService();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; object userViewObject = myService.GetUserDetails(&amp;quot;ravi&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Assert.IsNotNull(userViewObject, &amp;quot;Returned object can not be null&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;End calling GetUserDetails function&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;catch (Exception ex)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;throw new Exception(ex.ToString());&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;Here is part of service method.&lt;/p&gt;
&lt;p&gt;public UserView GetUserDetails(string userName)&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;	 &amp;nbsp; &amp;nbsp;try&lt;/p&gt;
&lt;p&gt;	 &amp;nbsp; &amp;nbsp;{			&lt;/p&gt;
&lt;p&gt;		 &amp;nbsp;string &amp;nbsp;loginName = null;&lt;/p&gt;
&lt;p&gt;		 &amp;nbsp; &amp;nbsp;foreach (ClaimSet claimSet in OperationContext.Current.ServiceSecurityContext.AuthorizationContext.ClaimSets)&lt;/p&gt;
&lt;p&gt;		 &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;			foreach (Claim claim in claimSet)&lt;/p&gt;
&lt;p&gt;			{&lt;/p&gt;
&lt;p&gt;			 &amp;nbsp; &amp;nbsp;if (claim.ClaimType == CsaClaimType.LoginName)&lt;/p&gt;
&lt;p&gt;			 &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;				loginName = (string)claim.Resource;&lt;/p&gt;
&lt;p&gt;			 &amp;nbsp; &amp;nbsp;}			 &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt;			}&lt;/p&gt;
&lt;p&gt;		 &amp;nbsp; &amp;nbsp;} &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/p&gt;
&lt;p&gt;	 &amp;nbsp; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt;	 &amp;nbsp; &amp;nbsp; .&lt;/p&gt;
&lt;p&gt;	 &amp;nbsp; &amp;nbsp; .&lt;/p&gt;
&lt;p&gt;	 &amp;nbsp; &amp;nbsp; .&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;OperationContext.Current works fine in service method if it is called from console application and and page when used in it. It it is called from unit test method OperationContext.Current returns null.&lt;/p&gt;</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#1887247</link><pubDate>Thu, 15 Mar 2007 16:32:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1887247</guid><dc:creator>ploeh</dc:creator><description>&lt;p&gt;Hi Ravi&lt;/p&gt;
&lt;p&gt;Thank you for sharing your code. However, since this is not a complete implementation, I can't reproduce your problem. Can you share a complete, simple repro of the problem?&lt;/p&gt;
</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#7076365</link><pubDate>Fri, 11 Jan 2008 19:42:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7076365</guid><dc:creator>Pav</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I was wondering is it possible to call the WCF service by adding the dll into the Test Project and maintain the Thread.CurrentPrinciple? Reason i ask is because i am testing a WCF Service and i want to bypass the proxy and call the service dll which has been referenced in my test project. I can successfully start debugging into the WCF service but i get an error when trying to assign the Thread.Principle.Name which is Null.&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Pav&lt;/p&gt;</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#7084500</link><pubDate>Sat, 12 Jan 2008 07:57:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7084500</guid><dc:creator>ploeh</dc:creator><description>&lt;p&gt;Hi Pav&lt;/p&gt;
&lt;p&gt;Thanks for writing.&lt;/p&gt;
&lt;p&gt;You can indeed unit test WCF services, as described in this post: &lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/ploeh/archive/2006/12/03/UnitTestingWCFServices.aspx"&gt;http://blogs.msdn.com/ploeh/archive/2006/12/03/UnitTestingWCFServices.aspx&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This effectively bypasses all WCF infrastructure, so you will need to manually populate Thread.CurrentPrincipal as part of your Fixture Setup. I've done that many times, and it works very well.&lt;/p&gt;
</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#7132431</link><pubDate>Wed, 16 Jan 2008 19:20:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7132431</guid><dc:creator>Pav</dc:creator><description>&lt;p&gt;Hi Ploeh,&lt;/p&gt;
&lt;p&gt;Thanks for your response&lt;/p&gt;
&lt;p&gt;Ahh of course in the Test Initialise Setup/Teardown, that makes sense!! I somehow thought the Current Thread was seperate in the two dll's and hence the CurrentPrinciple would not be persisted (duh!)&lt;/p&gt;
&lt;p&gt;Pav&lt;/p&gt;</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#7138201</link><pubDate>Thu, 17 Jan 2008 05:14:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7138201</guid><dc:creator>ploeh</dc:creator><description>&lt;p&gt;Hi Pav&lt;/p&gt;
&lt;p&gt;Good to hear that you were able to address your issue - I'm always happy to be able to help :)&lt;/p&gt;
</description></item><item><title>Unit tests for WCF (And Moq)</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#8513444</link><pubDate>Fri, 16 May 2008 18:15:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8513444</guid><dc:creator>Pablo M. Cibraro (aka Cibrax) </dc:creator><description>&lt;p&gt;As you may know, testing WCF services is not as simple as referencing a service implementation and start&lt;/p&gt;
</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#8643548</link><pubDate>Mon, 23 Jun 2008 22:55:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8643548</guid><dc:creator>Dave</dc:creator><description>&lt;p&gt;What is the best way to unit test WCF services when you have callbacks?&lt;/p&gt;</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#8663480</link><pubDate>Sat, 28 Jun 2008 15:51:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8663480</guid><dc:creator>ploeh</dc:creator><description>&lt;p&gt;Hi Dave&lt;/p&gt;
&lt;p&gt;Thank you for your question. Since I thought it was of general interest, I wrote a post about it: &lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/ploeh/archive/2008/06/28/unit-testing-duplex-wcf-services.aspx"&gt;http://blogs.msdn.com/ploeh/archive/2008/06/28/unit-testing-duplex-wcf-services.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;HTH&lt;/p&gt;
</description></item><item><title>re: Integration Testing WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#9060871</link><pubDate>Wed, 12 Nov 2008 01:11:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9060871</guid><dc:creator>Jeremy</dc:creator><description>&lt;p&gt;This was an extremely helpful and concise post.&lt;/p&gt;
&lt;p&gt;Thanks much,&lt;/p&gt;
&lt;p&gt;Jeremy&lt;/p&gt;</description></item><item><title>Unit Testing ADO.NET Data Services</title><link>http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#9314609</link><pubDate>Tue, 13 Jan 2009 15:05:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9314609</guid><dc:creator>ploeh blog</dc:creator><description>&lt;p&gt;ADO.NET Data Services enables you to expose data (including, but not limited to, relational data) as&lt;/p&gt;
</description></item></channel></rss>