Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you can add a bit more info on the code you are trying to test that would be great. Based on what you've written I can guess at a couple of options.</p> <p>Least invasive at the moment would probably be the approaches you suggested in your question: add an interface or common base class/adapter with the member you want to fake out as <code>virtual</code> so it can be picked up by mocking libraries that use dynamic proxy (like NSubstitute, Moq, FakeItEasy and Rhino Mocks). </p> <p>If you had something like <code>IInputControl&lt;T&gt;</code> which had <code>T Value { get; set;}</code> on it then you could substitute for that type in your tests. You would have to update the code to store references as <code>IInputControl&lt;T&gt;</code> rather than <code>TextBox</code>, but that may not be a bad thing as it isolates the code from the details of <code>TextBox</code>.</p> <p>The other option (not sure if you are doing this already) is to use a Model-View-Presenter style, and do not unit test the specifics of how the view translates messages into <code>TextBox</code> or other controls. (You could use acceptance tests for end to end testing.)</p> <p>As an example, say you had a presenter and view interface like this:</p> <pre><code>public class PersonPresenter { public PersonPresenter(IPersonView view, IPersonQuery query) { ... } public void Load() { var person = query.Execute(); view.Name = person.Name; view.Age = person.Age; } } public interface IPersonView { string Name { get; set; } int Age { get; set; } } </code></pre> <p>Your could test with something like this:</p> <pre><code>[Test] public void ShouldDisplayPersonOnLoad() { var view = Substitute.For&lt;IPersonView&gt;(); var query = Substitute.For&lt;IPersonQuery&gt;(); query.Execute().Returns(new Person("A", 20)); var subject = new PersonPresenter(view, query); subject.Load(); Assert.That(view.Name, "A"); Assert.That(view.Age, 20); } </code></pre> <p>Your actual view can then delegate to the existing controls. This is untested by unit tests, but might be acceptance testable, or be manually tested whenever the view is changed. Ideally this code should be simple enough to not hide too many bugs; the idea is to make the view as simple as possible, and to primarily be concerned with the appearance of the information, rather than with logic.</p> <pre><code>public PersonView : IPersonView { // ... public string Name { get { return nameTextBox.Value; } set { nameTextBox.Value = value; } } public int Age { get { return ageIntTextBox.Value; } set { ageIntTextBox.Value = value; } } } </code></pre>
    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.
    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