title: What is the best practice for writing RSpec tests/examples? Write examples to test the positive/affirmative, the negative, or both?
body: <p>Fairly new to BDD and RSpec, and I'm really curious as to what people typically do when writing their RSpec tests/examples, specifically as it relates to positive and negative tests of the same thing.</p>
  
  <p>Take for example validation for a username and the rule that a valid username contains only alphanumeric characters.</p>
  
  <p>The affirmative/positive test would be something like this:</p>
  
  <pre><code>it "should be valid if it contains alphanumeric characters"
    username = 'abc123'
    username.should be_valid
  end
  </code></pre>
  
  <p>While the negative test would be something like this:</p>
  
  <pre><code>it "should be invalid if it contains non-alphanumeric characters"
    username = '%as.12-'
    username.should_not be_valid
  end
  </code></pre>
  
  <p>Would you write one test, but not the other? Would you write both? Would you put them together in the same test? I've seen examples where people do any of the above, so I was wondering if there is a best practice and if so, what is it?</p>
  
  <p>Example of writing both positive and negative test:</p>
  
  <pre><code>it "should be invalid if it contains non-alphanumeric characters"
    username = '%as.12-'
    username.should_not be_valid
  
    username = 'abc123'
    username.should be_valid
  end
  </code></pre>
  
  <p>I've seen examples of people doing it this way, but I'm honestly not a fan of this approach. I tend to err on the side of keeping things clean and distinct with a single purpose, much like how we should write methods, so I would be more likely to write two separate tests instead of putting them together in one. So is there a best practice that states something of this sort? That examples should test a single feature/behavior from one angle, not all angles.</p>