Hacker News new | ask | show | jobs
by aaronbrethorst 4844 days ago
I hate to be that guy, but the Rspec tests should be written more like the following:

    describe 'Article' do
      let(:article) { Article.new }
  
      describe '#slug' do
        before(:each) do
          article.title = "Testing with Test::Unit"
        end
  
        it 'does not contain non-alphabetical characters' do
          article.slug.should_not match(/[^\w-]/)
        end
    
        it 'does not contain capital letters' do
          article.slug.should_not match(/[A-Z]/)
        end
    
        it 'replaces spaces with hyphens'
      end
    end
Note: I almost certainly misinterpreted the first Regex. 'now you have two problems,' etc. etc.
5 comments

one step further:

    describe Article do
      describe '#slug' do
        before do
          subject.title = "Testing with Test::Unit"
        end

        its(:slug) { should_not match(/[^\w-]/) }
        its(:slug) { should_not match(/[A-Z]/) }
        it 'replaces spaces with hyphens'
      end
    end
I like and use RSpec, but I think these improvements are proving the guy's point that its DSL is forbidding for beginners. I write RSpec more like the original post because it's less magic.
Not really. At least RSpec's creator doesn't think so (http://blog.davidchelimsky.net/2012/05/13/spec-smell-explici...).
neat, I didn't know you could do that. thanks for sharing!
Not at all. Feel free to be _that_ guy. First thing I noticed about the example was that it was badly written RSpec - which undermines an article advising on what you should/shouldn't do for testing.
Even better with the new syntax:

    expect(article.slug).to match(/[^\w-]/)
I didn't understand why the author used the unnecessary regex assertions, which are less clear and don't add any additional checks beyond:

  article.slug.should == "testing-with-test-unit"
I agree with you, I can't stand when I see @article and stuff strewn through out the tests. It's just asking for test pollution.