Note that there are some explanatory texts on larger screens.

plurals
  1. POModel depends on two repositories. How to mock one of them?
    text
    copied!<p>I have a <code>Project</code> entity that has a set of <code>Word</code> entities within. My <code>WordsRepository</code> depends on the <code>Project</code> entity. It should operate over the set of words that the project includes.</p> <pre><code>public interface IWordsRepository { List&lt;Word&gt; FetchAll(); } public class WordsRepository : IWordsRepository { private readonly Project _project; public WordsRepository(Project project) { if (project == null) throw new ArgumentNullException("project"); this._project = project; } public List&lt;Word&gt; FetchAll() { // code for retrieveing words for the project from the database } } </code></pre> <p>Now let's dig a little deeper. Here is my <code>ProjectsModel</code>. </p> <pre><code>public class ProjectsModel { private readonly IProjectsRepository _rep; private IWordsRepository _wordsRep; private IProjectsModelObserver _presenter; // Regular contructor for usual purpose public ProjectsModel() { this._rep = new ProjectsRepository(Program.context); } public ProjectsModel(IProjectsRepository repository) { this._rep = repository; } public virtual void AttachPresenter(IProjectsModelObserver observer) { this._presenter = observer; } public List&lt;Project&gt; projects { get { List&lt;Project&gt; tmpList = _rep.FetchAll(); return (tmpList != null) ? tmpList : new List&lt;Project&gt;(); } } private Project _selectedProject; public Project selectedProject { get { if (_selectedProject == null) _selectedProject = projects.FirstOrDefault(); return _selectedProject; } set { if (!projects.Contains(value)) throw new InvalidOperationException("Project not in the Projects list"); _selectedProject = projects[projects.IndexOf(value)]; // Recreating Words repository depending on project // Here is my issue: // As I should recreate the Words repository here, passing a new selected project // how can I mock it and make this class testable? this._wordsRep = new WordsRepository(this._selectedProject); if (this._presenter != null) this._presenter.SelectedProjectChanged(_selectedProject); } } private List&lt;Word&gt; _words; public List&lt;Word&gt; Words { get { // Fetching set of words in Project. Just invoking a words repository } } } </code></pre> <p>When the model's selected project is changed, it should recreate the <code>WordsRepository</code>, but it isn't testable this way. </p> <p>How can I mock it?</p>
 

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