How to test a method in an abstract class with abstract methods?

It is necessary to check the implementation of the virtual method "MyMethod" in the abstract "MyAbstractClass":

public abstract MyAbstractClass { public void MyMethod() { Testpassed = true; } public abstract int StatusCode{get;internal set;} // **EDIT**: internal setter was added public bool TestPassed{get;private set;} } 

I tried to do the following:

 [TestMethod()] { Mock<MyAbstractClass> mockClass = new Mock<MyAbstractClass>(); mockClass.Object.MyMethod(); Assert.IsTrue(mockClass.Object.TestPassed); } 

when trying to execute 'MyMethod' the following error is generated:

The method 'set_StatusCode' in type 'MyAbstractClass`2Proxy0434d60c0f4542fb9b4667ead4ca3a18' from the assembly 'DynamicProxyGenAssembly2 ", Version = 0.0.0.0, Culture = neutral, PublicKeyToken = null' has no implementation ..

Please advise how can I solve the problem?

Thank you so much!

PS Actually, I could inherit from MyAbstractClass and provide the implementation of the 'StatusCode' property, but I have a bunch of properties that will be implemented ...

+1
abstract-class moq
Aug 30 '10 at 22:31
source share
3 answers

See this answer for a partial mockery, and I think you will need to provide a specific implementation.

+1
Aug 30 '10 at 22:56
source share

The code you posted works fine (except for minor spelling errors). I simply could not make him fail with the same error. Moq implements the abstract property at run time and returns the default value (in this case, 0). Try a later version of Moq.

I would also caution against installing test logic in a class. If the sole purpose of TestPassed is to test than it definitely does not belong. We could help a lot more if you post the real code.

+2
Aug 31 '10 at 1:26
source share

I understand that this is not the answer to your question, but in any case. Even if you have many abstract properties, VS makes it extremely easy to add empty (actually casting) implementations. So, if MyMethod is independent of these design properties, I would add an empty derived class and test it.

+1
Aug 30 2018-10-10T00:
source share



All Articles