Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to unit test an async method from Silverlight 5
    primarykey
    data
    text
    <p>Edit: Context: I write a Silverlight application that accesses a Dynamics CRM 2011 via the Soap Service. I implemented a service for doing this. I want to write a unit test for this service now.</p> <p>I want to write a unit test for the following method:</p> <pre><code>public async Task&lt;List&lt;string&gt;&gt; GetAttributeNamesOfEntity(string entityName) { // build request OrganizationRequest request = new OrganizationRequest { RequestName = "RetrieveEntity", Parameters = new ParameterCollection { new XrmSoap.KeyValuePair&lt;string, object&gt;() { Key = "EntityFilters", Value = EntityFilters.Attributes }, new XrmSoap.KeyValuePair&lt;string, object&gt;() { Key = "RetrieveAsIfPublished", Value = true }, new XrmSoap.KeyValuePair&lt;string, object&gt;() { Key = "LogicalName", Value = "avobase_tradeorder" }, new XrmSoap.KeyValuePair&lt;string, object&gt;() { Key = "MetadataId", Value = new Guid("00000000-0000-0000-0000-000000000000") } } }; // fire request IAsyncResult result = OrganizationService.BeginExecute(request, null, OrganizationService); // wait for response TaskFactory&lt;OrganizationResponse&gt; tf = new TaskFactory&lt;OrganizationResponse&gt;(); OrganizationResponse response = await tf.FromAsync(result, iar =&gt; OrganizationService.EndExecute(result)); // parse response EntityMetadata entities = (EntityMetadata)response["EntityMetadata"]; return entities.Attributes.Select(attr =&gt; attr.LogicalName).ToList(); } </code></pre> <p>My first approach was to call the actual CRM. This failed because I cannot authenticate. I asked a question about this <a href="https://stackoverflow.com/questions/19496623/how-to-unit-test-soap-service-access-from-crm-2011-silverlight-application">here</a>. My second approach is to mock the organization service and run the method against my the mock. Like this:</p> <pre><code> private bool _callbackCalled = false; private SilverlightDataService _service; private List&lt;string&gt; names; [TestInitialize] public void SetUp() { IOrganizationService organizationService = A.Fake&lt;IOrganizationService&gt;(); _service = new SilverlightDataService {OrganizationService = organizationService}; IAsyncResult result = A.Fake&lt;IAsyncResult&gt;(); A.CallTo(organizationService.BeginExecute(A&lt;OrganizationRequest&gt;.Ignored, A&lt;AsyncCallback&gt;.Ignored, A&lt;object&gt;.Ignored)).WithReturnType&lt;IAsyncResult&gt;().Returns(result); EntityMetadata entities = new EntityMetadata(); AttributeMetadata meta = new AttributeMetadata(); meta.LogicalName = "foobar"; entities.Attributes = new ObservableCollection&lt;AttributeMetadata&gt;(); entities.Attributes.Add(meta); OrganizationResponse response = new OrganizationResponse(); response.Results = new ParameterCollection(); response["EntityMetadata"] = entities; A.CallTo(() =&gt; result.IsCompleted).Returns(true); A.CallTo(result.AsyncState).WithReturnType&lt;object&gt;().Returns(response); A.CallTo(organizationService.EndExecute(result)).WithReturnType&lt;OrganizationResponse&gt;().Returns(response); } [TestMethod] [Asynchronous] public async void TestGetAttributeNamesOfEntity() { TaskFactory&lt;List&lt;string&gt;&gt; tf = new TaskFactory&lt;List&lt;string&gt;&gt;(); names = await tf.FromAsync(_service.GetAttributeNamesOfEntity("avobase_tradeorder"), CallbackFunction); Assert.IsTrue(_callbackCalled); Assert.IsNotNull(names); } /// &lt;summary&gt; /// is used as callback for the method GetAttributeNamesOfEntity /// &lt;/summary&gt; /// &lt;param name="result"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; private List&lt;string&gt; CallbackFunction(IAsyncResult result) { _callbackCalled = true; names = (List&lt;string&gt;) result.AsyncState; return null; } </code></pre> <p>I use FakeItEasy for mocking. When I execute this test (with ReSharper and AgUnit) it waits forever. I also tried <a href="https://stackoverflow.com/questions/11513422/silverlight-async-unit-testing">this approach</a>, but it did not work. </p> <p>1) My assumption is that my mocks dont tell the await magic that the task is actually done. Why does the await never end? What do I have to do to fix this?</p> <p>2) Is there maybe a better approach to do this?</p> <p>Edit: I am using: VS 2012 Premium, ReSharper 7.1, Silverlight 5, AgUnit 0.7, .Net 4.5, FakeItEasy 1.13.1 installed via NuGet.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload