title: Using Moq to mock a repository that returns a value
body: <p>How do I set up my test method on that mocks a repository which accepts an object?</p>
  
  <p>This is what I have so far:</p>
  
  <p>Service.cs</p>
  
  <pre><code>    public int AddCountry(string countryName)
      {
          Country country = new Country();
          country.CountryName = countryName;
          return geographicsRepository.SaveCountry(country).CountryId;
      }
  </code></pre>
  
  <p>test.cs</p>
  
  <pre><code>    [Test]
      public void Insert_Country()
      {
          //Setup
          var geographicsRepository = new Mock&lt;IGeographicRepository&gt;();
  
          geographicsRepository.Setup(x =&gt; x.SaveCountry(It.Is&lt;Country&gt;(c =&gt; c.CountryName == "Jamaica"))); //How do I return a 1 here?
  
          GeographicService geoService = new GeographicService(geographicsRepository.Object);
  
          int id = geoService.AddCountry("Jamaica");
  
          Assert.AreEqual(1, id);
      }
  </code></pre>
  
  <p><code>SaveCountry(Country country);</code> returns an int.</p>
  
  <p>I need to do 2 things:</p>
  
  <ol>
  <li>First test, I need to tell the setup to return an int of 1.</li>
  <li><p>I need to create a second test <code>Insert_Duplicate_Country_Throws_Exception()</code>. In my Setup, how do I tell the repository to throw an error when I do:</p>
  
  <pre><code>int id = geoService.AddCountry("Jamaica");
  int id = geoService.AddCountry("Jamaica");
  </code></pre></li>
  </ol>
  
  <p>Framework:</p>
  
  <ol>
  <li>NUnit.</li>
  <li>Moq.</li>
  <li>ASP.NET MVC - repository pattern.</li>
  </ol>