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

static class File

3 Answers 78 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Rayne
Top achievements
Rank 1
Rayne asked on 19 Oct 2012, 03:18 PM

I'm having trouble mocking File.WriteAllBytes. Here's my test code:

var called = false;
var ccr = new Ccr { Pdf = new byte[73], SubmissionId = 3 };
Mock.Replace<string, byte[]>((s, b) => File.WriteAllBytes(s, b)).In<CcrFileWriter>(x => x.WriteToDisk(ccr));
Mock.Arrange(() => File.WriteAllBytes(null, null)).IgnoreArguments().DoInstead(()=> called = true);
var writer = new CcrFileWriter();
writer.WriteToDisk(ccr);
Assert.IsTrue(called);

It's not working. It's not bypassing File.WriteAllBytes and the test fails.
Maybe I'm not understanding how this should work?

3 Answers, 1 is accepted

Sort by
0
Rayne
Top achievements
Rank 1
answered on 19 Oct 2012, 06:54 PM
OK, so it does work...in my sample solution. But not in the solution I need it to work in. Not sure yet, what's going on.
0
Rayne
Top achievements
Rank 1
answered on 19 Oct 2012, 07:27 PM
I figured out what is causing the problem. The reason it's failing is because I'm using class Ccr as a parameter to the method I'm testing and that class is defined in a different assembly. I got the same failure in my sample project when I moved the classes there to a new project.

I don't know how to fix it though. How do I need to set up my test?

I'm using version 2012.3.1016.3 which I installed yesterday via the Control Panel.
0
Ricky
Telerik team
answered on 24 Oct 2012, 04:18 PM
Hi Rayne,

Thanks again for contacting us.

I have sent you a sample project in ticket # 571362 that should clarify the issue. In addition, make sure that you have updated all the references before running the test.

 

Kind Regards
Ricky
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
General Discussions
Asked by
Rayne
Top achievements
Rank 1
Answers by
Rayne
Top achievements
Rank 1
Ricky
Telerik team
Share this question
or