<?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>Unit Testing a WCF RIA DomainService: Part 3, The DomainServiceTestHost</title><link>http://blogs.msdn.com/b/kylemc/archive/2011/08/18/unit-testing-a-wcf-ria-domainservice-part-3-the-domainservicetesthost.aspx</link><description>In this thrilling conclusion to my series on unit testing, I’ll show you how to use the DomainServiceTestHost to test your DomainServices. In parts one and two I showed you how to extract external dependencies using the IDomainServiceFactory and how to</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Unit Testing a WCF RIA DomainService: Part 3, The DomainServiceTestHost</title><link>http://blogs.msdn.com/b/kylemc/archive/2011/08/18/unit-testing-a-wcf-ria-domainservice-part-3-the-domainservicetesthost.aspx#10321736</link><pubDate>Tue, 19 Jun 2012 13:00:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10321736</guid><dc:creator>Remco Blok</dc:creator><description>&lt;p&gt;Thanks Kyle. I got it working. I was a bit confused at first about the ChangeSetEntry class Id property. I first thought this was the primary key of the entity in the changset entry, but if I understand it correctly these id&amp;#39;s are generated on the client for each new changeset to allow associations in a changeset entry to reference the changeset that is the other side of the association, correct? It will also specify an order I assume in which the entries in a changeset need to be processed on the server, correct?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10321736" width="1" height="1"&gt;</description></item><item><title>re: Unit Testing a WCF RIA DomainService: Part 3, The DomainServiceTestHost</title><link>http://blogs.msdn.com/b/kylemc/archive/2011/08/18/unit-testing-a-wcf-ria-domainservice-part-3-the-domainservicetesthost.aspx#10319921</link><pubDate>Thu, 14 Jun 2012 15:02:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10319921</guid><dc:creator>kylemc</dc:creator><description>&lt;p&gt;@Remco&lt;/p&gt;
&lt;p&gt;Composition is one of those scenarios that didn&amp;#39;t make the initial cut. Unfortunately I don&amp;#39;t have sample lying around of the verbose way to test. Essentially you&amp;#39;ll have to call DomainService.Submit with a ChangeSet. To do that you need to do a number of things.&lt;/p&gt;
&lt;p&gt;(1) Write an IServiceProvider&lt;/p&gt;
&lt;p&gt;(2) Create a DomainServiceContext&lt;/p&gt;
&lt;p&gt;(3) Create a DomainService&lt;/p&gt;
&lt;p&gt;(4) Call DomainService.Initialize&lt;/p&gt;
&lt;p&gt;(5) Call DomainServiceDescription.GetDescription&lt;/p&gt;
&lt;p&gt;(6) Create a ChangeSetEntry for each parent and child entity&lt;/p&gt;
&lt;p&gt;(7) Create a ChangeSet containing those entries&lt;/p&gt;
&lt;p&gt;(8) Call DomainService.Submit&lt;/p&gt;
&lt;p&gt;Sorry for the inconvenience, but I hope this starts you in the right direction.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10319921" width="1" height="1"&gt;</description></item><item><title>re: Unit Testing a WCF RIA DomainService: Part 3, The DomainServiceTestHost</title><link>http://blogs.msdn.com/b/kylemc/archive/2011/08/18/unit-testing-a-wcf-ria-domainservice-part-3-the-domainservicetesthost.aspx#10319768</link><pubDate>Thu, 14 Jun 2012 07:58:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10319768</guid><dc:creator>Remco Blok</dc:creator><description>&lt;p&gt;Hi Kyle,&lt;/p&gt;
&lt;p&gt;I use composition and can&amp;#39;t get it to work with the DomainServiceTestHost. When you say &amp;#39;there may still be a few edge cases for association and composition change sets that are not supported&amp;#39;, do you mean composition is not supported at all or only certain edge cases of composition? Say I have the following Update method:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void UpdateLogbook(Logbook currentLogbook)&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;var originalLogbook = this.ChangeSet.GetOriginal(currentLogbook);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (originalLogbook == null)&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;this.Context.Logbooks.Attach(currentLogbook);&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;else&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;this.Context.AttachAsModified(currentLogbook, originalLogbook);&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;foreach (var logbookEntry in&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.ChangeSet.GetAssociatedChanges(currentLogbook, logbook =&amp;gt; logbook.LogbookEntries))&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;var operation = this.ChangeSet.GetChangeOperation(logbookEntry);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;switch (operation)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &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; &amp;nbsp; &amp;nbsp;case ChangeOperation.Insert:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.Context.AttachAsAdded(logbookEntry);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;break;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;case ChangeOperation.Update:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.Context.AttachAsModified(logbookEntry, this.ChangeSet.GetOriginal(logbookEntry));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;break;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;case ChangeOperation.Delete:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.Context.AttachAsDeleted(logbookEntry);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;break;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &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;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;How can I use the DomainServiceTestHost to test adding a new LogbookEntry to the Logbook.LogbookEntries collection? The following test method, which I expected not to work, throws an exception.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;[TestMethod]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void GivenNewLogbookEntryWhenInsertingThenShouldAddLogbookEntry()&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;var logbookEntry = new LogbookEntry();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;LogbookEntry addedLogbookEntry = null;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.domainContextMock.Setup(t =&amp;gt; t.AttachAsAdded(It.IsAny&amp;lt;LogbookEntry&amp;gt;())).Callback&amp;lt;LogbookEntry&amp;gt;(t =&amp;gt; addedLogbookEntry = t);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.domainServiceTestHost.Insert(logbookEntry);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Assert.AreSame(logbookEntry, addedLogbookEntry);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;I suspect I&amp;#39;ll need to manually create a ChangeSet somehow and submit that. Could you provide some guidance on that please?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10319768" width="1" height="1"&gt;</description></item><item><title>re: Unit Testing a WCF RIA DomainService: Part 3, The DomainServiceTestHost</title><link>http://blogs.msdn.com/b/kylemc/archive/2011/08/18/unit-testing-a-wcf-ria-domainservice-part-3-the-domainservicetesthost.aspx#10276382</link><pubDate>Fri, 02 Mar 2012 11:12:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10276382</guid><dc:creator>Simon</dc:creator><description>&lt;p&gt;Hi Kyle,&lt;/p&gt;
&lt;p&gt;Yes indeed it does thanks&lt;/p&gt;
&lt;p&gt;I just added a new reposity for EF and injected that instead of your mock repository, and all good&lt;/p&gt;
&lt;p&gt;Thanks again&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10276382" width="1" height="1"&gt;</description></item><item><title>re: Unit Testing a WCF RIA DomainService: Part 3, The DomainServiceTestHost</title><link>http://blogs.msdn.com/b/kylemc/archive/2011/08/18/unit-testing-a-wcf-ria-domainservice-part-3-the-domainservicetesthost.aspx#10273536</link><pubDate>Mon, 27 Feb 2012 18:00:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10273536</guid><dc:creator>kylemc</dc:creator><description>&lt;p&gt;@Simon&lt;/p&gt;
&lt;p&gt;I think I addressed your concern about how to use repositories in this post (&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/b/kylemc/archive/2011/08/18/unit-testing-a-wcf-ria-domainservice-part-2-the-repository-pattern.aspx"&gt;blogs.msdn.com/.../unit-testing-a-wcf-ria-domainservice-part-2-the-repository-pattern.aspx&lt;/a&gt;). It shows both test and production-focused implementations.&lt;/p&gt;
&lt;p&gt;Also, the full source for this sample is available on code.msdn (&lt;a rel="nofollow" target="_new" href="http://code.msdn.microsoft.com/Unit-Testing-a-WCF-RIA-a39a700e"&gt;code.msdn.microsoft.com/Unit-Testing-a-WCF-RIA-a39a700e&lt;/a&gt;).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10273536" width="1" height="1"&gt;</description></item><item><title>re: Unit Testing a WCF RIA DomainService: Part 3, The DomainServiceTestHost</title><link>http://blogs.msdn.com/b/kylemc/archive/2011/08/18/unit-testing-a-wcf-ria-domainservice-part-3-the-domainservicetesthost.aspx#10273453</link><pubDate>Mon, 27 Feb 2012 14:46:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10273453</guid><dc:creator>Simon C</dc:creator><description>&lt;p&gt;Hi Kyle,&lt;/p&gt;
&lt;p&gt;This is an excellent post which successfully consolidates concepts/patterns that are loosely explain elsewhere with reference to WCFRIA.&lt;/p&gt;
&lt;p&gt;I modified the source so that the repository got data from a real db, via the Entity Framerwork, in essence returning &amp;#39;real&amp;#39; book entities. This was done by refactoring the method (public MockBookRepository()), and essentially allowed me to switch to &amp;#39;real&amp;#39; entities once I have successfully tested with mock entities. &lt;/p&gt;
&lt;p&gt;This is great as it allows a staged development whereby I can move to real entities once i am happy that everything works with test data.&lt;/p&gt;
&lt;p&gt;What I am having problems with is getting the update/insert/delete to work in the same way, i.e. adjuting the update so this updates the entities in the entity framework, as opposed to the mock in house repositor.&lt;/p&gt;
&lt;p&gt;This would make the frameowkr great as could do all CRUD testing with mock data and then switch to the real data context (ef and database) once testing was successfull. &lt;/p&gt;
&lt;p&gt;Can you please advise as to how this can be done please?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10273453" width="1" height="1"&gt;</description></item><item><title>re: Unit Testing a WCF RIA DomainService: Part 3, The DomainServiceTestHost</title><link>http://blogs.msdn.com/b/kylemc/archive/2011/08/18/unit-testing-a-wcf-ria-domainservice-part-3-the-domainservicetesthost.aspx#10236379</link><pubDate>Sat, 12 Nov 2011 00:31:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10236379</guid><dc:creator>kylemc</dc:creator><description>&lt;p&gt;@Alan&lt;/p&gt;
&lt;p&gt;When you use the repository pattern, it&amp;#39;s best not to use the wizard. There&amp;#39;s enough that&amp;#39;s significantly different that it would just be a waste of your time. That pattern&amp;#39;s not a necessity for testing, though. It&amp;#39;s just a common approach to abstracting your tests from a database. As an alternative approach, you could use standard generated code and just make sure to point it at a test database. There are a number of options that I outlined in this series, but there&amp;#39;s nothing forcing you to use them in any specific combination.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10236379" width="1" height="1"&gt;</description></item><item><title>re: Unit Testing a WCF RIA DomainService: Part 3, The DomainServiceTestHost</title><link>http://blogs.msdn.com/b/kylemc/archive/2011/08/18/unit-testing-a-wcf-ria-domainservice-part-3-the-domainservicetesthost.aspx#10235592</link><pubDate>Thu, 10 Nov 2011 01:25:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10235592</guid><dc:creator>Alan Rutter</dc:creator><description>&lt;p&gt;I am researching using WCF RIA Services for some upcoming work and was concerned about testing. This is an excellent series of articles. There appears to be a lot of modifications to the DomainService as generated by the Domain Service Wizard - I am concerned that whenever our database/model changes, does this mean we cannot use the wizard or have to manually update the generated files?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10235592" width="1" height="1"&gt;</description></item><item><title>re: Unit Testing a WCF RIA DomainService: Part 3, The DomainServiceTestHost</title><link>http://blogs.msdn.com/b/kylemc/archive/2011/08/18/unit-testing-a-wcf-ria-domainservice-part-3-the-domainservicetesthost.aspx#10214123</link><pubDate>Tue, 20 Sep 2011 15:35:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10214123</guid><dc:creator>kylemc</dc:creator><description>&lt;p&gt;@William&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t have any particular recommendations. I always start at a page like silverlight.net and go from there. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10214123" width="1" height="1"&gt;</description></item><item><title>re: Unit Testing a WCF RIA DomainService: Part 3, The DomainServiceTestHost</title><link>http://blogs.msdn.com/b/kylemc/archive/2011/08/18/unit-testing-a-wcf-ria-domainservice-part-3-the-domainservicetesthost.aspx#10210212</link><pubDate>Tue, 13 Sep 2011 20:29:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10210212</guid><dc:creator>william simons </dc:creator><description>&lt;p&gt;Thanks for your posts they are always very educational! Did you ever think about writing a book about mvvm, design patterns, prism, unity or mef using dependency injection. &lt;/p&gt;
&lt;p&gt;Do you have any favorite books that you could reccommend? &amp;nbsp; &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10210212" width="1" height="1"&gt;</description></item></channel></rss>