Coder Social home page Coder Social logo

Comments (6)

Ergamon avatar Ergamon commented on June 15, 2024 2

Sadly it seems still something must be different. I tried to run it with some self imagined IEvent and INotification interfaces. Now even with exactly the ones you posted, I dont get any error.

Basically I did create a new .NET 7 console app, added NSubstitute NuGet package and pasted in basically your code:

using NSubstitute;

internal class Program
{
  private static INotificationRecipientsProvider<CreatedEvent, Source> _recipientProviderMock;

  static void Main(string[] args)
  {
    WithRecipients(Enumerable.Empty<string>());
  }

  private static void WithRecipients(IEnumerable<string> recipients)
  {
    _recipientProviderMock = Substitute.For<INotificationRecipientsProvider<CreatedEvent, Source>>();
    _recipientProviderMock
        .NotificationRecipients(Arg.Any<CreatedEvent>())
        .Returns(x => recipients?.ToList());
  }
}

public interface IEvent<T>
{
  string Description { get; }

  T Payload { get; }
}

public interface INotification
{
  string NotificationType { get; }

  long Key { get; }

  string Operation { get; }

  IDictionary<string, object> Properties { get; }

  INotification CreateSnapshot();
}

public interface INotificationRecipientsProvider<T, E>
    where T : EventBase<E>
    where E : class
{
  IList<string> NotificationRecipients(T notificastionEvent);
}

public abstract class EventBase<T> : IEvent<T>, INotification
{
  protected EventBase(T payload)
  {
    Payload = payload;
  }

  public abstract string Description { get; }

  public T Payload { get; }

  public abstract string NotificationType { get; }
  
  public abstract long Key { get; }

  public abstract string Operation { get; }

  public abstract IDictionary<string, object> Properties { get; }

  public abstract INotification CreateSnapshot();
}

public class Source
{

}

public class CreatedEvent : EventBase<Source>
{
  public CreatedEvent(Source source)
    : base(source)
  {
  }

  public override string Description => throw new NotImplementedException();

  public override string NotificationType => throw new NotImplementedException();

  public override long Key => throw new NotImplementedException();

  public override string Operation => throw new NotImplementedException();

  public override IDictionary<string, object> Properties => throw new NotImplementedException();

  public override INotification CreateSnapshot()
  {
    throw new NotImplementedException();
  }
}

And I dont get any excpetion. So it is hard to help, if you cannot reproduce the error.

from nsubstitute.

Ergamon avatar Ergamon commented on June 15, 2024

I am lacking the definitions for IEvent and INotification.
Can you provide a working sample to reproduce or at least tell what other sources are needed?

from nsubstitute.

kchristo71 avatar kchristo71 commented on June 15, 2024

@Ergamon although I am not sure these interfaces are in any way interesting (meaning you could remove the inheritance) this is IEvent definition
public interface IEvent<T> { string Description { get; } T Payload { get; } }
And this is INotification:
public interface INotification { string NotificationType { get; } long Key { get; } string Operation { get; } IDictionary<string, object> Properties { get; } INotification CreateSnapshot(); }
I hope this helps, if not please let me know what else is missing

from nsubstitute.

kchristo71 avatar kchristo71 commented on June 15, 2024

@hi Ergamon. Thank you for your time. I have done a similar setup, extracting all the classes to a simple test project and the setup does not fail. I even you the same events and entities as payload for the event. This is a concern as this means that there is some reference or project configuration in the real life scenario that causes this weird behaviour.

from nsubstitute.

kchristo71 avatar kchristo71 commented on June 15, 2024

Hi @Ergamon I finally managed to sort this out. I had to download NSubstitute latest code and debug.
It was a very tricky one. With Moq it is a common practice to use statements like:

T x = It.IsAny<T>(); which results in setting the default value of T.

This is used to "declare" that we do not care for the value x and can be any value.
Doing the equivalent (T x = Arg.Any<T>();) SHOULD NEVER be used with NSubstitute because it adds T as argument definition in the thread context and will be added to the potential argument matches for the next setup.

This is where Int32 was coming from

from nsubstitute.

kchristo71 avatar kchristo71 commented on June 15, 2024

Thank you for looking into this @Ergamon and I have to apologise for not providing all the test setup code, but it seemed really irrelevant and "innocent".

from nsubstitute.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.