chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I have some code that will cast a type (SPField) to one of its parent types such as SPFieldNumber or SPFieldChoice.

Problem is I do not know in the test which is going to be which to take, in SharePoint they are all stored as SPField and then you can unbox them with the proper type.

When I do a Isolate.Fake.Instance<SPField>() and pass that in I get an exception cannot cast type SPField to SPFieldNumber which is obviously incorrect since of course it can cast that... If I change it to SPFieldNumber it works fine...my current fix is an ugly if statement with 'is' and casting to make it work..is there a more elegant approach to this problem?
asked by emalamisura (1.2k points)

1 Answer

0 votes
Hi,

I'm not sure that I understand the problem.
Do you get a compilation error or error in runtime?
If it's a runtime error please post the test code and the exception message you get including the stack trace.

However if it's a compilation error it seems like a language limitation
For example the code below will not compile.
public class Foo 
{ }

public class Bar : Foo
{}

public class UnderTest
{
    public void F(Bar bar)
    {}
}

[Test, Isolated]
public void Test()
{
    var fake = Isolate.Fake.Instance<Foo>();

    UnderTest underTest = new UnderTest();
    underTest.F(fake); // <-- compilation error here.
}
answered by ohad (35.4k points)
...