Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your test looks very strange - you are exercising mocked object instead of testing some real object which will be used by your application. If you are testing <code>Question</code> object, then you should use exactly same instance of <code>Question</code> type, as your application would use. What should be mocked - dependencies of <code>Question</code>. Because classes should be unit-tested in isolation (otherwise problems with dependencies would result in failing tests of <code>Question</code> which works fine).</p> <p>So, if you have <code>Question</code> which displays something on console (i.e. it depends on <code>Console</code>), then unit-testing requires mocking of this dependency. You can't mock <code>Console</code> with Moq, because it's static class. Thus you should create abstraction for console, which will be used by <code>Question</code>:</p> <pre><code>public interface IConsole { void Write(string message); } </code></pre> <p>Now inject this dependency to your <code>Question</code>:</p> <pre><code>public class Question { private IConsole _console; private string _message; public class Question(IConsole console, string message) { _console = console; } } </code></pre> <p>With this code you can write tests for <code>Question</code> behavior:</p> <pre><code>[Test] public void ShouldAskQuestionOnConsole() { var message = "Hello World"; var consoleMock = new Mock&lt;IConsole&gt;(); consoleMock.Setup(c =&gt; c.Write(message)); var question = new Question(consoleMock.Object, message); question.Ask(); consoleMock.VerifyAll(); } </code></pre> <p>This test specifies, that question should send its message to console, when <code>Ask</code> is executed. Implementation is simple:</p> <pre><code>public void Ask() { _console.Write(_message); } </code></pre> <p>Now you have working <code>Question</code>. And you should implement <code>IConsole</code> which will be used by your application. It's simple wrapper:</p> <pre><code>public class ConsoleWrapper : IConsole { public void Write(string message) { Console.WriteLine(message); } } </code></pre> <p>In your real application inject this implementation to question (this will can be done automatically by dependency injection framework):</p> <pre><code>IConsole console = new ConsoleWrapper(); Question question = new Question(console, message); </code></pre> <p>Notes: I'd go with some interface like <code>IView</code> instead of <code>IConsole</code>. That will completely abstract <code>Question</code> class from type of <code>UI</code> it works with. And second note is separating of business logic from presentation logic. Usually you don't have class which is responsible for two things - holding data of question (and possibly processing it), and interacting with user. Usually there is something like controller, which receives user input, refreshes UI and asks business model for data.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload