2011-02-10 15 views
5

Próbuję śledzić ten artykuł (http://blog.zoolutions.se/post/2010/04/01/Conquering-NServiceBus-part-4-e28093-Testing.aspx), aby utworzyć test jednostkowy dla mojej sagi nservicebus projektJak utworzyć testy jednostkowe dla Sagi NServiceBus?

Zobacz poniższy kod, nie wiesz, dlaczego zawsze narzeka ktoś wie, jak mogę to naprawić?

(używam nservice autobus 2.0)

public class ReportSaga : Saga<ReportSagaData>, 
          IAmStartedByMessages<RequestReportMessage>, 
          IHandleMessages<PollingReportStatusMessage> 
{ 
// implementation 
} 



[TestFixture] 
    public class ReportSaga_HandleRequestReportMessageTests 
    { 
     [TestFixtureSetUp] 
     public void SetUp() 
     { 
      var assemblies = new[] 
         { 
          typeof (ReportSaga).Assembly, 
          typeof (RequestReportMessage).Assembly, 
          typeof (PollingReportStatusMessage).Assembly, 
          Assembly.Load("NServiceBus"), 
          Assembly.Load("NServiceBus.Core") 
         }; 

      Test.Initialize(assemblies); 
     } 

     [Test] 
     public void HandleRequestReportMessageTests() 
     { 

      Test.Handler<ReportSaga>() 
       .OnMessage<RequestReportMessage>(x => 
       { 
        x.Id = 1234; 
        x.ReportDate = DateTime.Now; 
       }); 


     } 
    } 


Test 'UnitTests.ReportSaga_HandleRequestReportMessageTests.HandleRequestReportMessageTests' failed: System.ArgumentException : GenericArguments[0], 'ReportSagaData', on 'NServiceBus.IMessageHandler`1[T]' violates the constraint of type 'T'. 
    ----> System.TypeLoadException : GenericArguments[0], 'ReportSagaData', on 'NServiceBus.IMessageHandler`1[T]' violates the constraint of type parameter 'T'. 
    at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e) 
    at System.RuntimeType.MakeGenericType(Type[] instantiation) 
    at NServiceBus.Testing.Test.Handler[T](T handler) 
    at NServiceBus.Testing.Test.Handler[T]() 
    ReportSaga_HandleRequestReportMessageTests.cs(34,0): at UnitTests.ReportSaga_HandleRequestReportMessageTests.HandleRequestReportMessageTests() 
    --TypeLoadException 
    at System.RuntimeTypeHandle.Instantiate(RuntimeTypeHandle handle, IntPtr* pInst, Int32 numGenericArgs, ObjectHandleOnStack type) 
    at System.RuntimeTypeHandle.Instantiate(Type[] inst) 
    at System.RuntimeType.MakeGenericType(Type[] instantiation) 

0 passed, 1 failed, 0 skipped, took 1.11 seconds (NUnit 2.5.5). 

Odpowiedz

7

W celu przetestowania saga, trzeba zadzwonić Test.Saga zamiast Test.Handler.

8

Dokładnie jak powiedział Udi, jednak składnia powinna wyglądać mniej więcej tak:

[TestFixture] 
public class ReportSaga_HandleRequestReportMessageTests 
{ 
    [TestFixtureSetUp] 
    public void SetUp() 
    { 
     var assemblies = new[] 
        { 
         typeof (ReportSaga).Assembly, 
         typeof (RequestReportMessage).Assembly, 
         typeof (PollingReportStatusMessage).Assembly, 
         Assembly.Load("NServiceBus"), 
         Assembly.Load("NServiceBus.Core") 
        }; 

     Test.Initialize(assemblies); 
    } 

    [Test] 
    public void HandleRequestReportMessageTests() 
    { 

     var message = new RequestReportMessage { Id = 1234, ReportDate = DateTime.Now }; 

     Test.Saga<ReportSaga>() 
      .ExpectPublish<PublishMessage>(e => e.SomePropertyOfPublishMethod == "value") 
      .When(x => x.Handle(message)); 

    } 
}