Different keywords to test in ruby? describe vs test

I'm a little confused about testing RSpec / Integration in Ruby.

I've noticed that some tests start with a keyword test and some start with a description? What's the main difference? is it all RSpec?

eg

 test "email should be present" do
      @user.email = "     "
      assert_not @user.valid?
 end

      

and

describe 'can set' do
  it 'calories' do
    @dessert.calories = 80
  end
end

      

Edit: MiniTest vs RSpec Pros and Cons?

+3


source to share


3 answers


The syntax test

... assert

is typical of MiniTest, sometimes also called Test Unit.

The syntax describe

... it

is typical for RSpec.

However, MiniTest has two kinds of syntax, depending on your personal preference:



  • test

    ... assert

    which is the classic syntax; he uses Rails for his own internal testing.

  • syntax describe

    ... it

    which is called MiniTest Spec; it looks a lot like a simple subset of RSpec syntax.

If you're working on an existing project, it's good to write using the same syntax as the rest of the project, so your teammates can read the syntax easily.

(Your board has asked for and against) is a question that suits Google searches better than for because there is something to be said about it and there are so many strong opinions. Both are great choices and in most projects you will do great using what is already used in the project, or your teammates know the best.)

+4


source


The syntax test

is in MiniTest , (Test :: Unit in older Ruby versions) in the ruby ​​standard library. describe

/ it

are taken from Rspec.



0


source


If you are asking why / how to use, please describe:

the description is helpful because you can test several things that are related without repeating. For example, if you want to test that the desert can set calories and flavor, you can do:

describe 'desserts' do
  it 'can set calories' do
    @dessert.calories = 80
    ...test...
  end
  it 'can set taste' do
    @dessert.taste = 'delicious'
    ...test...
  end
end

      

When this fails, you will get a message like "Unsuccessful desserts can set calories ..."

Instead:

  it 'dessert can set calories' do
    @dessert.calories = 80
    ...test...
  end
  it 'dessert can set taste' do
    @dessert.taste = 'delicious'
    ...test...
  end

      

where it is less readable and you repeat the word dessert.

0


source







All Articles