Should I write rail tests with the def or test keyword?

This seems like a simple question, but I cannot find the answer anywhere. I noticed that in general tests in a Ruby on Rails application can be written as:

  test "the truth" do
    assert true
  end

or

  def the_truth
    assert true
  end

It seems that the new material writes tests in the first way, but I can not find a reason for this. Does anyone prefer another? Is it correct? Thanks.

+5
source share
4 answers

In recent years, there has been a shift from short abbreviated test names to longer phrase-like names. This is partly due to the popularity of RSpec and the concept that tests are specifications and should be descriptive.

, test. , .

test "should not be able to login with invalid password" do
  #...
end

def_should_not_be_able_to_login_with_invalid_password
  #...
end

, , . def , .

+8

, , Rails 2.2. , ( def , ). !

+2

, RSpec . , :

Shoulda is a macro structure for writing concise unit tests for your models / controllers, and the second is replacing fixtures.

0
source

I would suggest testing with RSpec or Cucumbers . I use both to test all my applications. RSpec is used to test models and controllers, and Cucumber checks Views (via the included Webrat ).

-1
source

All Articles