c# - Prism TDD Unsubscribe() -


i'm using tdd , want write unit test unsubscribe() method available in pubsubevent. because there no interface inherit parent class, has no interface, don't know how test it.

my service , method, test:

public class frameservice: iframeservice {      private readonly ieventaggregator _eventaggregator;       public void unsubscribeevents()      {          _eventagregator.getevent<frameaddedevent>()                         .unsubscribe(frameaddedeventhandler); // how unit test this?       }  } 

the frameaddedevent class, inherit pubsubevent prism library:

public class frameaddedevent: pubsubevent<frame> { } 

declaration in prism's library:

public class pubsubevent<tpayload> : eventbase {       public subscriptiontoken subscribe(action<tpayload> action); } 

my test (using mstest , moq) first part of code line. need unittest assert on unsubscribe()

[testclass] public class frameservicetest {     private mock<ieventagregator> _eventagregator;      [testmethod]     public void when_subscribeevents_then_get_frameaddedevent_from_eventaggregator()     {         var frameaddedevent = new frameaddedevent();          _eventagregator.setup(x=>x.getevent<frameaddedevent>())                                    .returns(frameaddedevent);          _frameservice.subscribeevents();          _servicelayereventagregator.verify(x => x.getevent<frameaddedevent>(), times.once);     } } 

answer: see comment below explanation, i'm adding code people might have same problem me.

the fake class:

public class fakeframeaddedevent : frameaddedevent {     public bool unsubscribed { get; private set; }      public fakeframeaddedevent()     {         unsubscribed = false;     }      public override void unsubscribe(action<frame> subscriber)     {         unsubscribed = true;     } } 

and new unittest:

    [testmethod]     public void when_unsubscribeevents_then_unsubscribe_is_call()     {         var frameaddedevent = new fakeframeaddedevent();          _servicelayereventagregator.setup(x => x.getevent<frameaddedevent>())                                    .returns(frameaddedevent);          _frameservice.unsubscribeevents();          check.that(frameaddedevent.unsubscribed).istrue();     } 

first of all, pubsubevent part of prism , brings own set of tests, doubt necessity write own tests that.

that being said, can test stuff around eventaggregator, including own events, see this example.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -