title: Correct usage of Theory in xUnit
body: <p>I've been reading about xunit and I'm a bit confused how to use Theory.</p>
  
  <p>Lets say I have this test:</p>
  
  <pre><code>[Theory]
  [InlineData("")]
  [InlineData("anything")]
  public void AddItem_NoName_ThrowsNoNameException(string name)
  {
      // Arrange.
      ItemService itemService = new ItemService();
      Item item = new Item();
      item.Name = name;
  
      // Act.
      Exception ex = Record.Exception(() =&gt; itemService.AddItem(item));
  
      // Assert.
      Assert.NotNull(ex);
      Assert.IsType&lt;NoNameException&gt;(ex);
  }
  </code></pre>
  
  <p>And the method under test:</p>
  
  <pre><code>public void AddItem(Item item)
  {
      if (item.Name == "")
          throw new NoNameException();
      _DAL.AddItem(item);
  }
  </code></pre>
  
  <p>So this creates two unit tests: one passing an argument of empty string ("") and  one the word "anything". So the first one (the empty string) passes since we have code that checks if item.Name is empty string, throw exception. The second one however, fails, because Assert.NotNull(ex) in the unit test will be false. However, if you look at the AddItem method, the method is correctly coded so what I want to see is both tests pass.</p>
  
  <p>Maybe I'm wrong in how I implemented by unit test but what I want to happen or what I think should happen in this case is that both test should pass since the method under test is correct (based on the rules). How do I do this?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>OK, I've managed to make this both pass by doing the code below. However, I think that is not correct, to have conditional assertion. Still need help.</p>
  
  <pre><code>[Theory]
  [InlineData("", false)]
  [InlineData("anything", true)]
  public void AddItem_NoName_ThrowsNoNameException(string name, isValid)
  {
      // Arrange.
      ItemService itemService = new ItemService();
      Item item = new Item();
      item.Name = name;
  
      // Act.
      Exception ex = Record.Exception(() =&gt; itemService.AddItem(item));
  
      // Assert.
      if (!isValid)
      {
          Assert.NotNull(ex);
          Assert.IsType&lt;NoNameException&gt;(ex);
      }
      else
      {
          Assert.Null(ex);
      }
  }
  </code></pre>