Cannot assign to <method name> because it is a method group: Error (Using Moles framework)

StackOverflow https://stackoverflow.com/questions/14110648

  •  13-12-2021
  •  | 
  •  

Pregunta

These two lines of code give me the error as mentioned in the title. I am trying to define a stub for TestClass

var stubTestClass = new StubsTutorial.Moles.STestClass();
stubTestClass.WriteStuff = () => "Moled in static class!";

And this is how the function WriteStuff() is defined

public class TestClass {
  public string WriteStuff() {
    return "Stuff written in main class.";
  }
}

I am not sure where I am going wrong. And direction would help me.

¿Fue útil?

Solución

From a tutorial I found:

Each mole type is named by prepending an “M” to its name and placing it into a .Moles namespace.

So maybe your line

var stubTestClass = new StubsTutorial.Moles.STestClass();

should be changed to

var moleTestClass = new StubsTutorial.Moles.MTestClass();

though I have no experience with Microsoft Moles.

Otros consejos

I'm not a Moles user but...

"By default, stubs are generated for all interfaces and abstract classes, although you can also configure Moles to generate stubs for non-abstract classes that expose virtual methods"

So

a) have you configured correctly b) I note the method is not marked virtual if a) is correct

i.e.

public virtual string WriteStuff() { ... }

because enclosing class is concrete (not marked abstract or interface)

Stubs create their fake delegates for public abstract and virtual methods by overriding them. If the method is not marked virtual or abstract, any attempt to assign the stub delegate with a value will be overruled by the compiler because the method was not overriden to return an assigned delegate. Try it by stubbing a test class with two methods, one simply public and the other public virtual. In order to test a public class with no interface contract or abstract or virtual methods, shims must be used, not stubs.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top