static class File

4 posts, 0 answers
  1. Rayne
    Rayne avatar
    241 posts
    Member since:
    Sep 2010

    Posted 19 Oct 2012 Link to this post

    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?

  2. Rayne
    Rayne avatar
    241 posts
    Member since:
    Sep 2010

    Posted 19 Oct 2012 Link to this post

    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.
  3. DevCraft R3 2016 release webinar banner
  4. Rayne
    Rayne avatar
    241 posts
    Member since:
    Sep 2010

    Posted 19 Oct 2012 Link to this post

    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.
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 24 Oct 2012 Link to this post

    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.

Back to Top