title: Are for loops that test a condition OK in unit tests?
body: <p>I am starting to play around with xUnit and Moq and was wondering if I am doing it right.</p>
  
  <p>Should I write tests like this:</p>
  
  <pre><code>[Fact]
  public void ForWebShouldReturnDev()
  {
      // Arrange
      var server = new Mock&lt;HttpServerUtilityBase&gt;();
      server.SetupSequence(s =&gt; s.MachineName).Returns("t11466").Returns("localhost").Returns("webdev");
  
      var context = new Mock&lt;HttpContextBase&gt;();
      context.SetupGet(c =&gt; c.Server).Returns(server.Object);
  
      var service = new EnvironmentService(context.Object, null);
  
      // Act / Assert
      service.GetApplicationEnvironment().Should().Be(ApplicationEnvironment.Dev);
      service.GetApplicationEnvironment().Should().Be(ApplicationEnvironment.Dev);
      service.GetApplicationEnvironment().Should().Be(ApplicationEnvironment.Dev);
  }
  </code></pre>
  
  <p>Or more like this:</p>
  
  <pre><code>[Fact]
  public void ForWebShouldReturnDev()
  {
      // Arrange
      var server = new Mock&lt;HttpServerUtilityBase&gt;();
  
      var context = new Mock&lt;HttpContextBase&gt;();
      context.SetupGet(c =&gt; c.Server).Returns(server.Object);
  
      var service = new EnvironmentService(context.Object, null);
  
      // Act / Assert
      foreach (var name in new string[] { "t11466", "localhost", "webdev" })
      {
          server.Setup(s =&gt; s.MachineName).Returns(name);
          service.GetApplicationEnvironment().Should().Be(ApplicationEnvironment.Dev);
      }
  }
  </code></pre>
  
  <p>I know you are only supposed to test one unit of code per test, but when there are variables to the test is it OK to loop through asserts?</p>
  
  <p>I am having trouble figuring out the best way to test these types of things.</p>
  
  <p>For the record, below is the actual code under test:</p>
  
  <pre><code>// The constructor for the application environment
  public EnvironmentService(HttpContextBase httpContext, IEnvironment environment)
  {
      this.environment = environment;
      this.httpContext = httpContext;
  }
  
  private readonly Dictionary&lt;ApplicationEnvironment, string&gt; applicationEnvironments = new Dictionary&lt;ApplicationEnvironment, string&gt;() {
      { ApplicationEnvironment.Dev, "DEV" },
      { ApplicationEnvironment.Test, "TEST" },
      { ApplicationEnvironment.QA, "QA" },
      { ApplicationEnvironment.Prod, "PROD" }
  };
  
  public ApplicationEnvironment GetApplicationEnvironment()
  {
      var machine = IsWeb ? httpContext.Server.MachineName : environment.GetCommandLineArgs().First();
  
      return applicationEnvironments.FirstOrDefault(x =&gt; machine.ToUpper().Contains(x.Value)).Key;
  }
  </code></pre>
  
  <p>I have another question out there <a href="https://stackoverflow.com/questions/12484241/setup-moq-to-return-multiple-values">here</a> regarding more specifics with Moq.</p>
  
  <p>Are there any good resources out there for Moq, their documentation is pretty thin.</p>