nsubstitute/NSubstitute — Issue #66: Received() should keep a copy arguments to the call
Author: robdmoore
Created: 2012-02-03 05:19:10+00:00
State: open

Hopefully my test is fairly self-explainatory. I realise this isn't easy to do. Perhaps using something like ValueInjector could be used though?

using NSubstitute;
using NUnit.Framework;

namespace SomeNamespace
{
[TestFixture]
public class NSubstituteShould
{
[Test]
public void Keep_copy_of_arguments_for_received_calls_rather_than_reference()
{
// Arrange
var s = Substitute.For();
var c = new Concrete(s);
var entity = new Entity {Property = "before"};

// Act
c.Action(entity);

// Assert
s.Received().Save(entity); // You can test references like this
s.Received().Save(Arg.Is(l => l.Property == "before")); // I want this to be a copy of the object when the call was made
}
}

public class Concrete
{
private readonly IService _service;

public Concrete(IService service)
{
_service = service;
}

public void Action(Entity entity)
{
_service.Save(entity);
entity.Property = "after";
}
}

public interface IService
{
void Save(Entity entity);
}

public class Entity
{
public string Property { get; set; }
}
}