2017-02-09 5 views
1

私のテストでは少し苦労しています。ここでMStestのタスク結果はnullです

は私が

public async Task Handle(ReceiveEventsFromSalesForceCommand message, IMessageHandlerContext context) 
{ 
    var queryResult = await this.GenerateQueryResultAsync(message).ConfigureAwait(false); 

    await this.DetermineActionAsync(context, queryResult).ConfigureAwait(false); 
} 

public async Task<QueryResult<EventStore__c>> GenerateQueryResultAsync(ReceiveEventsFromSalesForceCommand message) 
{ 
    QueryResult<EventStore__c> queryResult; 
    if (string.IsNullOrWhiteSpace(message.NextRecordsUrl)) 
     { 
      queryResult = await this.forceClient.QueryAsync<EventStore__c>(query).ConfigureAwait(false); 
      this.log.Info($"AFTER: QueryAllAsync<EventStore>(query), found {queryResult?.TotalSize ?? 0} records"); 
     } 
     else 
     { 
      queryResult = await this.forceClient.QueryContinuationAsync<EventStore__c>(message.NextRecordsUrl).ConfigureAwait(false); 
      this.log.Info("AFTER: QueryContinuationAsync<EventStore>(query)"); 
     } 
    return queryResult; 
} 

をテストしていたコードであり、これは

[TestMethod] 
    public async Task Test() 
    { 
     // arrange 
     var forceConfig = Substitute.For<ISalesForceCreationHandlerConfig>(); 
     var forceClient = Substitute.For<IForceClient>(); 
     forceClient.QueryAllAsync<EventStore__c>(Arg.Any<string>()).Returns(Task.FromResult(new QueryResult<EventStore__c> { NextRecordsUrl = "Dummy" })); 
     var messageHandlerContext = Substitute.For<IMessageHandlerContext>(); 
     var handler = new SalesForceBatchCreationHandler(forceClient, null, forceConfig); 

     // act 
     await handler.Handle(new ReceiveEventsFromSalesForceCommand(), messageHandlerContext); 

     // assert 
     await messageHandlerContext.Received().Send(Arg.Is<ReceiveEventsFromSalesForceCommand>(command => string.IsNullOrWhiteSpace(command.NextRecordsUrl)), Arg.Any<SendOptions>()); 
     await messageHandlerContext.DidNotReceive().SendLocal(Arg.Any<PublishMultipleKlantManagementEnvelopeCreatedEventsCommand>()); 
    } 

私の問題は私のGenerateQueryResultAsync方法のiresultがnullであると私はとNullReferenceExceptionを得ることである私のユニットテストです。結果がnullでないことを確認して例外を回避するにはどうすればよいですか?

+1

サイドノート:最初のメソッド 'Handle'では、async/awaitを省略すると効率的です。そのメソッドで完了を待つ理由がないので(メソッドに表示されていないものがない限り)、タスクを直接返してください。 – Igor

+1

これは、http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-itの複製として閉じられない理由はありますか?あなたが必要とするところで、単にnull以外の値を持つことができないということは、あなたのケースを特別なものにしているのですか? – nvoigt

+1

NS substitute 1.9.0以降を使用していますか?あなたは 'QueryAllAsync'をスタブしているようですが、SUTは' QueryAsync'を呼び出しています。 –

答えて

0

非同期呼び出しの方法を再構成します。おそらくこの{queryResult.TotalSize}が原因です。

public async Task<QueryResult<EventStore__c>> GenerateQueryResultAsync(ReceiveEventsFromSalesForceCommand message) { 
    QueryResult<EventStore__c> queryResult; 
    if (string.IsNullOrWhiteSpace(message.NextRecordsUrl)) { 
     queryResult = await this.forceClient.QueryAsync<EventStore__c>(query).ConfigureAwait(false); 
     this.log.Info($"AFTER: QueryAllAsync<EventStore>(query), found {queryResult?.TotalSize ?? 0} records"); 
    } else { 
     queryResult = await this.forceClient.QueryContinuationAsync<EventStore__c>(message.NextRecordsUrl).ConfigureAwait(false); 
     this.log.Info("AFTER: QueryContinuationAsync<EventStore>(query)"); 
    } 
    return queryResult; 
} 
+0

私はハンドルメソッドを編集しました、私は別のメソッドの入力としてqueryresultが必要です。実際にはqueryResult.TotalSizeが原因でした。しかし、それが返すqueryresultはnullになりました。私のユニットテストで何らかの構成が欠けているような気がするのですか? –

+0

@RonaldRozema次に、 'forceClient.QueryAsync'メソッドを見直してデバッグし、nullを返す理由を調べる必要があります。このメソッドの中には、nullの結果が返されるものがあります。 – Nkosi

関連する問題