Rspec VS. Testunit

Difference between Unit::Test versus Rspec

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.

Rspec vs. TestUnit

Both Test::Unit and Rspec are very nice solutions. As for me I prefer to use RSpec in my Rails project - because it has many automatic solutions like autotest, highlighting syntax, shortcut commands like rake spec and other stuff that make using test easer. But in my plain ruby scripts I use Test::Unit - it is very lightweight and staightforward when you don't need complex testing (integrational and others)

Using Rspec in one file like test/unit

You just need to tell the RSpec::Core::Runner to run your spec.

Adding RSpec::Core::Runner.run([$__FILE__]) at the end of your fill should work.

Updated code:

require 'rspec'

describe "Anagrams" do
def anagrams(word, words)
words.select { |w| w.chars.sort == word.chars.sort }
end

it "should only match the anagrams" do
anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada']) == ['aabb', 'bbaa']
end
end

RSpec::Core::Runner.run([$__FILE__])

Minitest and Test::Unit

Basically, Test::Unit got replaced by MiniTest (which ships with Ruby now). MiniTest tests can be written in the Test::Unit syntax or in the MiniTest::Spec syntax. Mini::Test, as a standalone, doesn't have a future... but MiniTest is faster, smaller, included by default, and has a great future and, IMHO, you should definitely make the switch! Although it is definitely lacking in documentation, as you have found. For the most part, you can just use Test::Unit documentation for help with using MiniTest.

How can I tell Rails to use RSpec instead of test-unit when creating a new Rails app?

The following should work:

at command line:

rails new MYAPP -T # The -T option tells rails not to include Test::Unit

in Gemfile:

gem 'rspec-rails'

at command line:

bundle install
rails g rspec:install

Test::Unit should equivalent of RSpec should_receive

Check out mocha. It is a gem that you can use with Test::Unit or Rspec (but I prefer the native rspeck mocking). It allows you to write something like

@post.stubs(:something).returns(:bla)
@post.expects(:something_else).raises(StandardError, "failed")

Hope this helps



Related Topics



Leave a reply



Submit