4

I get the Oops! code message when submitting the question below, any idea why?

Oops! Your question couldn't be submitted because:

Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon.

Moq throws a TypeInitializationException when using Moles with the [HostType("Moles")]. Code example and exception below. 


    [TestMethod]
    [HostType("Moles")]
    public void TestMethod()
    {
        var mock = new Mock<IAnything>();
    }

The exception:

<pre>
System.TypeInitializationException occurred
  Message=The type initializer for 'Moq.Extensions' threw an exception.
  Source=Moq
  TypeName=Moq.Extensions
  StackTrace:
       at Moq.Extensions.IsMockeable(Type typeToMock)
       at Moq.Mock`1.CheckParameters()
       at Moq.Mock`1..ctor(MockBehavior behavior, Object[] args)
       at Moq.Mock`1..ctor(MockBehavior behavior)
       at Moq.Mock`1..ctor()
       at BP.DFR.Common.Services.Network.DfrNetworkQueryTests.MyTestMethod()
  InnerException: System.Security.VerificationException
       Message=Operation could destabilize the runtime.
       Source=Moq
       StackTrace:
            at Moq.Extensions..cctor()
       InnerException:
</pre>


The link below suggested it might be a moq version issue, but it happens in both version 3.1 and 4.0 for me.

http://code.google.com/p/moq/issues/detail?id=344

I'm using VS 2010 Premium SP1. Anybody had any luck resolving this?
1
  • I've also tried escaping the [HostType("Moles")] with backticks, but no joy.
    – SDK
    Commented Aug 13, 2012 at 8:15

1 Answer 1

4

Looking at your code, you probably haven't indented below line properly:

System.TypeInitializationException occurred

You need to indent your code by four spaces, or select it all and press the {} button. Also make sure there is one blank line between your last line of text and your first line of code. This will cause your code to be rendered in <pre><code> blocks and all of its HTML/Markdown entities will be escape/ignored respectively.

Properly indented code renders like this:

#include <iostrea>

int main() {
  vector<int> items;
}

Unindented code renders like this:

include

int main() { vector items; }

You can see why we stress proper indenting of code.

3
  • No, the part of the code beginning with System.TypeInitializationException occurred is within a <pre> block, that works too. Commented Aug 10, 2012 at 16:32
  • Yip the System.TypeInitializationException is not code, just an exception message, that's why I put in the <pre> block. I don't know if it has something to do with the [HostType("Moles")] in the first line being seen as code. But I don't really want that line as code. Thanks for the reply, still no joy though :(
    – SDK
    Commented Aug 11, 2012 at 10:11
  • I didn't actually try your suggestion, thought Gilles was correct with his comment. But you're right the exception message needs to be indented. I wouldn't have considered it code, but hey. Thanks.
    – SDK
    Commented Aug 13, 2012 at 8:23

Not the answer you're looking for? Browse other questions tagged .