c# - Constructing mocks in unit tests -


Is there a way to create a duplicate instead of a real example, when the test code that calls the constructor?

For example:

  Public Class ClassToTest {Public Zero MethodToTest () {MyObject foo = new MyObject (); Console.WriteLine (foo.ToString ()); }}  

In this example, I need to prepare a unit test that confirms that by calling Methodistost on an example of classostost, actually a new constructed toasting () The result of the method will be the example of MyObject.

I can not see a way to actually test the 'Classroom Test' class in different classes; The test of this method will actually test the method 'MyObject.ToString ()' as well as the MethodToTest method.

Not really your classster test is difficult to test due to its design If you want to be able to swap your dependencies for the mock objects, then you need to think about how these dependencies are injected - but there is no way to build them in the constructure, which is your help. is. You can use constructor injection:

  public class classost {MyObject _foo; Public Zero MethodToTest (Myobject foo) {_foo = foo; Console.WriteLine (foo.ToString ()); }}  

Or you can use property injection:

  public class ClassToTest {public MyObject Foo {get; Set; } Public Zero MethodToTest () {}}  

The former may be in your example and you want to write a console there; This is often the best way when your dependence should depend on an object instead of an option.

You can do much worse than searching for your concepts.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -