vendredi 29 janvier 2016

Rhino Mocks - Arg

I am using Rhino Mocks to stub out the functionality of a SOAP endpoint and for the most part it works. But, the interface is quite quirky and I am struggling to get the following to work (i have renamed the classes for simplicity) public interface IWebService { void CopyFile(Request request); }

public interface IService
{
    void CopyFile(string filename, byte[] data);
}

public class Request
{
    public string Filename { get; set; }
    public byte[] Data { get; set; }
}
public class Service : IService
{
    IWebService _service;
    public Service(IWebService service)
    {
        _service = service;
    }
    public void CopyFile(string filename, byte[] data)
    {
        _service.CopyFile(new Request() {Filename = filename,Data = data });
    }
}

Now, in my test I have something like this

[TestCase]
        public void TestFileCopyFailsIfFilenameIsMissing()
        {
            IWebService serviceMock = MockRepository.GenerateMock<IWebService>();
            serviceMock.Expect(x => x.CopyFile(Arg<Request>.Is.Equal(new Request() { Filename = Arg<string>.Is.Null, Data = Arg<byte[]>.Is.Anything }))).Throw(new Exception());

            Service service = new Service(serviceMock);
            service.CopyFile(null, new byte[] { });

        }

Which throws the exception: An exception of type 'System.InvalidOperationException' occurred in Rhino.Mocks.dll but was not handled in user code

Additional information: Use Arg ONLY within a mock method call while recording. 1 arguments expected, 3 have been defined.

I have tried the all possibilities in the world on this one, but cant get it right. If i dont use Arg and use

Expect(null, new byte[]{});

it will always pass no matter what

Aucun commentaire:

Enregistrer un commentaire