I am interested in Test::Unit
and Rspec
.
Could someone explain me what is the main difference between the two - in terms of principles on which they operate.
I am interested in Test::Unit
and Rspec
.
Could someone explain me what is the main difference between the two - in terms of principles on which they operate.
Test::Unit is more akin to a classic TDD tool like JUnit. Tests are written as classes (because that's how it was done in Java / C++).
Test::Unit is more approachable by those who are used to classic testing tools.
Test::Unit
has been completely replaced by Minitest.
require_relative "simple_number"
require "test/unit"
class TestSimpleNumber < Test::Unit::TestCase
def test_add
assert_equal(4, SimpleNumber.new(2).add(2) )
end
def test_multiply
assert_equal(6, SimpleNumber.new(2).multiply(3) )
end
end
RSpec is a domain specific language which uses the flexibility of the ruby language to create tests which are "more readable".
Earlier versions of RSpec took this a bit too far and monkeypatched core Ruby classes.
RSpec is more oriented towards the philosophy of Behavior Driven Development where you describe the behavior of the components in your app vs TDD where you slice the app into its smallest components.
require "spec_helper"
require "lib/simple_number"
describe SimpleNumber do
describe "#add" do
it "adds a value to the sum" do
expect(SimpleNumber.new(2).add(2)).to eq(4)
end
end
describe "#multiply" do
it "multiplies with the value" do
expect(SimpleNumber.new(2).multiply(3)).to eq(6)
end
end
end
Which is better?
Thats a matter of opinion.
Minitest has recently had a bit of a resurgence as it is perceived as faster and simpler. However tests can be extremely terse, and what really slows down a suite is integration tests and database IO.
A well written RSpec test suite (or spec suite) is both more readable and easy to navigate. When a spec fails it is pretty easy to tie it to what actually went wrong in the application. Of course there are many suites which are the exact opposite.
Minitest is extremely flexible and can even be used with a spec style syntax. Its very sparse out of the box and does not include a rich assertions library and a mocking library like RSpec. While you can plug anything in you still need to get all the moving parts to work together.
RSpec has a much better test runner and superior documentation.
© 2022 - 2024 — McMap. All rights reserved.