开发者

Mocking an array of type with Rhino Mock

开发者 https://www.devze.com 2022-12-10 10:43 出处:网络
I am having issues mocking an array with Rhino Mock, any direction would be great. namespace Checks_Rhino_Mocks

I am having issues mocking an array with Rhino Mock, any direction would be great.

namespace Checks_Rhino_Mocks
{
    public class Check
    {
        public Header header;
        public Detail[] details;
    }

    public cl开发者_如何学Pythonass Header
    {
        public string Number;
        public decimal Amount;
    }

    public class Detail
    {
        public string Id;
    }


    [TestFixture]
    public class CheckUT
    {
        [Test]
        public void CheckShouldHaveMultipleDetails()
        {
            MockRepository mock = new MockRepository();

            Check check = mock.StrictMock<Check>();
            check.header = mock.StrictMock<Header>();
            //issue
            check.details = mock.StrictMock<Detail[]>();
        }
    }
}


You can't mock Check.details because it's not virtual. RhinoMocks, Moq, etc, can't mock non-virtual methods.

To solve this, make the field virtual:

public class Check
{
    public virtual Header header;
    public virtual Detail[] details;
}

But...and here's the real point: why are you trying to mock a Detail array? What are you trying to do, exactly? Explain what you're trying to do and we'll be able to really help you.


When creating the check details, you will probably have to do it with IEnumerable:

check.details = mock.StrictMock<IEnumerable<Detail>>();

instead of an array...


why not just create an array? is there a reason you want to mock an array? i'd think you could just use a real array and assert that you have whatever elements (which could be mock instances) in the array you want. it's hard for me to imagine the test you couldn't do with just a real array.

0

精彩评论

暂无评论...
验证码 换一张
取 消