Hacker News new | past | comments | ask | show | jobs | submit login

(I commented on your gist but I'll comment here as well for completeness.)

It might be a bit easier for your brain if you think of specs not as "Ruby-language scripts", but as "RSpec-language scripts", a language which is a superset of Ruby.

RSpec is a DSL — domain-specific language — on top of Ruby. It purposely pollutes the top-level namespace with shorthand methods so that you don't have to qualify them. Yes, the range of what is available is essentially something you will need to hunt down in the documentation. And yes, the available shorthands are also dependent on what RSpec extensions you have installed; things like WebMock extend the namespace with its own methods.

After the confusion settles I think you will find that the reduced syntax is very helpful. It removes boilerplate to a large extent. In a more verbose test framework such as Test::Unit, developers always move common boilerplate (eg., test setup, common mocks) into test-wide helpers. RSpec just sets the table in a way that also reduces common test boilerplate.

For example, surely `describe Foo` is better than `class FooTest < Test::Unit::TestCase` — even if you don't understand what `describe` really does or where it comes from. You don't need to if you accept it's RSpec syntax.

And surely you will agree that this:

    get "/"
    last_response.status.should eq 200
...more concisely expresses the intent of the test than this:

    request = Net::HTTP.get('localhost', '/')
    response = http.request request
    assert_equal response.code, 200
I personally quite like writing assertions as infix-style expressions (`x.should y`) rather than assertions (`assert_equal`). One reason is the wonderful flexibility in choosing what goes on the left and right sides:

    lambda {
      client.connect!
    }.should_not raise_error(Client::ConnectionError)
Or:

    stub = stub_request(:get, '/template').with({
      query: hash_including(name: 'signup'),
      headers: {
        'Content-Type' => 'application/json'
      }
    })
    get "/signup"
    stub.should have_been_requested



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: