This is a migrated thread and some comments may be shown as answers.

Any way to assert whether a particular constructor was called?

3 Answers 56 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 07 Apr 2011, 10:26 AM
Hi,

let's take the following method which saves a MemoryStream to a file:

public void Write(MemoryStream stream, string fileName)
{
    using (FileStream fs = new FileStream(fileName, FileMode.Create))
    {
        stream.WriteTo(fs);
    }
}

I would like to write a unit test that ensures that the the FileStream instance in this method was created with the particular constructor (string path, FileMode mode) and that correct parameters were passed to it (the value of fileName and FileMode.Create in this case). At the same time, I'd like that the FileStream instance created in this method does nothing, i.e. that nothing is saved to the file system upon calling this method in a unit test. So essentially, I would need to mock the FileStream object so that it a) does nothing and b) I can check whether the particular constructor was called with particular parameters.

Any way to achieve this using JustMock (commercial edition)?

Thanks, Robert

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 08 Apr 2011, 01:49 PM
Hello Robert,
Thank you for sending the issue. Unfortunately, since you can't arrange a constructor therefore it would be hard to assert if a constructor is called with particular arguments. In that case you could write the test in the following way:

[ClassInitialize]
public static void Initialize(TestContext context)
{
    Mock.Partial<MemoryStream>().For<Stream>((x, i) => x.WriteTo(i));
}
 
[TestMethod]
public void TestMethod1()
{
    var stream = new MemoryStream();
 
    string path = String.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("dd-MM-yyy-hh-mm-ss"));
     
    Mock.Arrange(() => stream.WriteTo(Arg.IsAny<Stream>())).MustBeCalled();
     
    using (var filStream = new FileStream(path, FileMode.Create))
    {
       stream.WriteTo(filStream);
 
    }
 
    Mock.Assert(() => stream.WriteTo(Arg.IsAny<Stream>()), Occurs.Exactly(1));
}

However, faking constructor for specific arguments is a great idea and we would love to include it in one of coming releases. Once included,  you will then be able to do the following to fake any constructor and assert it later on (like you do it for static methods):

Mock.Arrange(() => new FileStream(string.Empty, FileMode.Create)).DoNothing();


Thanks again for using JustMock and Please write us back for any other issues that you might get.


Kind Regards,
Ricky
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Vladi
Telerik team
answered on 08 Apr 2011, 03:40 PM
Hello Robert,

Thank you for your valuable feedback which turns out to be cool feature request as Ricky pointed out. I've entered it in our public issue tracking system (PITS) and you could follow and vote for it here: http://www.telerik.com/support/pits.aspx#/public/justmock/5469. We will notify you as the feature is ready and implemented.

Besides this as we like the feature request we would like to award you some Telerik points. Please review your account to see the updated Telerik points.

Let us know if you have any further questions or comments.

Greetings,
Vladi
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Robert
Top achievements
Rank 1
answered on 08 Apr 2011, 03:55 PM
Hi Vladi & Ricky,

thanks for your answers and the points :)

Great that you could take this feature onto your todo list.

Regards, Robert
Tags
General Discussions
Asked by
Robert
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Vladi
Telerik team
Robert
Top achievements
Rank 1
Share this question
or