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

MsCorlib Mocking with a static class: is it possible?

4 Answers 126 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
DenisCL
Top achievements
Rank 1
DenisCL asked on 16 Aug 2010, 11:32 AM
Hi,

I've been trying to mock calls the the System.Environment class in MsCorlib.

I followed the guidelines in the documentation:
  • I created the class with the MockClass attribute:
    [TestClass, MockClass]
    public class UnitTest1
  • I tried to use the partial method:
    Mock.Partial<Environment>().For<string>((x) => Environment.GetFolderPath(Arg.IsAny<Environment.SpecialFolder>()));
But this doesn't feel right (the lambda uses an instance but the class is static) and it does not compile for the same reason: cannot use static types as type arguments.

Is there a way to mock static mscorlib classes? (Environment.GetFolderPath)

Thanks!
DenisCL

4 Answers, 1 is accepted

Sort by
0
Accepted
Ricky
Telerik team
answered on 16 Aug 2010, 04:36 PM
Hi DenisCL,

Thanks for asking the question. As Environment is a static type, you can't use it directly as a generic argument. The compiler simply doesn't allow that. Also, looking at your test, I would like to inform you that Arg.IsAny does not have any effect at Mock.Partial that only setups the interceptor for mocking the specified framework member.

As an example let me rewrite the test, just to give a head start.

Firstly, in test initialization  you will partially setup the target member for mocking in the following way:

[ClassInitialize] 
 public static void Initialize(TestContext context) 
 
     // static mscorlib members other than File, DateTime 
     Mock.Partial(typeof(Environment)) 
         .For<Environment.SpecialFolder, string>((i, j) => Environment.GetFolderPath(i)); 
 }


Then, the rest is similar to mocking any other member, where you add your criteria for that member using helpers like Arg.IsAny<T>():

[TestMethod]
public void ShouldMockMembersFromEnviroment()
{
    string expected = "ping";
    Mock.Arrange(() => Environment.GetFolderPath(Arg.IsAny<Environment.SpecialFolder>())).Returns(expected);
    Assert.Equal(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), expected);
}


Hope the information is useful.

Best wishes,
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
DenisCL
Top achievements
Rank 1
answered on 17 Aug 2010, 09:08 AM
Hi,

Thanks for the answer!

However, I am having trouble using the Partial method the way you pointed out.
In my version (2010.1.713.7 - le last one I can get my hands on), I cannot find an overload of Partial that takes a Type as a parameter.
Only the generic one is available: 
public static MockInitializer<T> Partial<T>()


Is this a feature that will be available in the next version?

Thanks,
DenisCL
0
Accepted
Ricky
Telerik team
answered on 17 Aug 2010, 09:23 AM
Hi DenisCL,

Thanks again for replying. The feature is added in SP1 release and the version number is 2010.2 810. I would request you to download the lastest build that is available through your telerik account.




Regards,
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
DenisCL
Top achievements
Rank 1
answered on 17 Aug 2010, 12:47 PM
You guys are releasing updates quite often, that's great.

It solves my problem,
Thank you!

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