Почему Observable.Do выдает InvalidOperationException «Последовательность не содержит элементов»C#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Почему Observable.Do выдает InvalidOperationException «Последовательность не содержит элементов»

Сообщение Anonymous »


I do get an (IMHO) unexpected Exception InvalidOperationException "Sequence contains no elements" in the below code, where I believe this should not be the case. Please see the code, background info and description below.

The Issue

For the sake to reproduce it easily and to demonstrate it, the code in the Unit Test using my SUT Effect is commented out, and replaced with the given code which should make it clear what I am trying to do and what I am trying to test.

So, I can reduce the question to "Why do I get a "Sequence contains no elements" exception when executing Observable.Do, when it is a total valid use case":

IObservable observable = Observable.Empty(); And, I want to test this, an empty sequence which completes.

Unit Test

public class EffectTests { [Fact] public async void TestEmpty() { // IObservable observable = Effect.Empty.Invoke() // .Delay(System.TimeSpan.FromSeconds(1.2)); IObservable observable = Observable.Empty() .Delay(System.TimeSpan.FromSeconds(1.2)); // Exception has occurred: CLR/System.InvalidOperationException // Exception thrown: 'System.InvalidOperationException' in System.Reactive.dll: 'Sequence contains no elements.' // at System.Reactive.Subjects.AsyncSubject`1.GetResult() // at EffectTests.d__0.MoveNext() in await observable.Do( onNext: _ => AssertEx.Fail() ); } } public static class AssertEx { public static void Fail(string message = "") => throw new Xunit.Sdk.XunitException(message); } Background

I hope, the code in comments and the xUnit test setup shows what I am trying to do:

I have a custom struct Effect which basically stores a function returning an IObservable. This should show already the fact that an Effect can actual return any kind of observable (via function Invoke()). That observable may now return values that will be emitted synchronously or asynchronously, it might fail immediately or succeed, or it might emit one or more values, or none at all.

In my Unit Test, in the case of an Effect.Empty the Observable is actually an empty sequence and thus it's intended to not emit any values. So, I precicely added an Assert.Fail() when the callback onNext would be called.

Additional Info

So, the documentation to Observable.Do says:

// Summary: // Invokes an action for each element in the observable sequence, and propagates // all observer messages through the result sequence. This method can be used for // debugging, logging, etc. of query behavior by intercepting the message stream // to run arbitrary actions for messages on the pipeline. which clearly does not state anything that it would throw an exception if the sequence is empty. It also cleary states the use case, which is for "debugging, logging, etc.", so I thought that would be a perfect fit for my use case, i.e. Unit Tests.

My Questions
  • Why does Observable.Do throw the exception?
  • How can I alleviate the issue in my Unit Tests?

There are a lot of questions about "Sequence contains no elements" on SO already, but as far as I could look it up, there is none specifically mention it in regards to Observable.Do. But any existing answer is appreciated, too.


Источник: https://stackoverflow.com/questions/751 ... ains-no-el
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C#»