Automated Silverlight WCF asynchronous test

Using Silverlight NUnit + statlight really makes silverlight developing much safer, theoretically, at least for those really UNIT tests.

With the concern of integration tests, thing becomes a little bit difficult. At first I thought I could just write WCF test like this:


        [Test]
        public void Should_call_find_posts_by_keywords()
        {
            var client = new MyServiceClient();
            client.FindPostsByKeywordCompleted += (sender, args) => {};
        }

The first problem I encountered is, I couldn’t get the client config in Silverlight UnitTest project. Tried adding linked file and adding service reference, no good. My ugly workaround is to hardcode the endpoint address for now:


        [Test]
        public void Should_call_find_posts_by_keywords()
        {
            var client = new MyServiceClient(new System.ServiceModel.BasicHttpBinding(),
                                        new EndpointAddress(
                                            "http://localhost/WcfServices/PostService.svc"));
            client.FindPostsByKeywordCompleted += (sender, args) => {};
        }

OK, First problem solved. Now, another one, statlight couldn’t report the exception if service got something wrong. While in Visualstudio the exception alert can always pop-up.

Something needs be done for asynchronous test. After read the posts from Jeff Wilcox and Jonas Folleso, I ended up with my test looks like this:


 [TestFixture]
    public class ProxyTests : SilverlightTest
    {
        [Test]
        [Asynchronous]
        public void Should_call_find_posts_by_keywords()
        {
            var client = new VineOnlineServiceClient(new System.ServiceModel.BasicHttpBinding(),
                                        new EndpointAddress(
                                            "http://localhost2/WcfServices/PostService.svc"));

            bool isSearchDone = false;
            IEnumerable<PostedItemDto> searchResults = null;

            client.FindPostsByKeywordCompleted += (
                (sender, args) =>
                    {
                        searchResults = args.Result;
                        isSearchDone = true;
                    });

            EnqueueCallback(() => client.FindPostsByKeywordAsync("post"));
            EnqueueConditional( () => isSearchDone );

            EnqueueCallback( () => Assert.IsNotNull(searchResults));  // Optional if we don't care the return data

            EnqueueTestComplete(); // Cannot move to teardown coz asynchronous
        }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s