Rieles: Cuando se realizan pruebas unitarias, ¿se desean "fallas" o "errores" al principio?

Hi I'm trying to learn testing and TDD, so I started a new project and made a User model. I made a User modelo con: name, gender, age.

class User < ActiveRecord::Base
  attr_accessible :age, :gender, :name

I read that TDD is about making tests (that fail), fixing them, and then running the tests again to see them pass. So does that mean I want a failure first or error or do either work?

I'm asking because I just wrote a second test:

require 'test_helper'

class UserTest < ActiveSupport::TestCase
  test "user is created" do
    user = User.create(:name => "Edmund", :age => 3, :gender => "m")
    assert_equal user, User.find_by_name("Edmund")
    assert_equal 3, User.count

  test "user has sent messages" do
    user = User.create(:name => "Edmund", :age => 3, :gender => "m")
    2.times do 
      user.sent_messages.create(:sender_id => user.id)
    assert_equal 2, user.sent_messages.count


for sent messages. I ran rake test:units, expecting it to be a failure since I don't have any Message model nor any association with has_many :messages en mi User model, however I got an error:

Edmunds-MacBook-Pro:langoexchange edmundmai$ rake test:units
NOTICE:  CREATE TABLE will create implicit sequence "users_id_seq" for serial column "users.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "users_pkey" for table "users"
Run options: 

# Running tests:


Finished tests in 0.087926s, 22.7464 tests/s, 22.7464 assertions/s.

  1) Error:
NoMethodError: undefined method `sent_messages' for #<User:0x007fa73bbd68e0>
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:407:in `method_missing'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:149:in `method_missing'
    /Users/edmundmai/Desktop/Class/Ruby/langoexchange/test/unit/user_test.rb:13:in `block (2 levels) in <class:UserTest>'
    /Users/edmundmai/Desktop/Class/Ruby/langoexchange/test/unit/user_test.rb:12:in `times'
    /Users/edmundmai/Desktop/Class/Ruby/langoexchange/test/unit/user_test.rb:12:in `block in <class:UserTest>'

2 tests, 2 assertions, 0 failures, 1 errors, 0 skips
rake aborted!
Command failed with status (1): [ruby -I"lib:test" -I"/Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/rake-10.0.2/lib" "/Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/rake-10.0.2/lib/rake/rake_test_loader.rb" "test/unit/**/*_test.rb" ]
Tasks: TOP => test:units
(See full trace by running task with --trace)

Is this (error) the expected outcome of TDD or did I do something wrong or interpret the process of TDD incorrectly?

preguntado el 04 de diciembre de 12 a las 04:12

2 Respuestas

Yes, this is the expected first step in TDD. Once you've written your first test(s), your next task is to get your code up to the point where you get actual fallas en contraposición a errores. A failure is different from an error because it means that your test was expecting something, and that expectation was not met. In the case of an error, the expectation never even gets a chance to be tested because something breaks in the code under test. (In your case, the spec never gets a chance to check whether user.sent_messages.count es igual a 2 porque sent_messages is not even defined.)

The TDD cycle typically focuses on getting from red (failure) to green (success + refactor) because this is the important part of the process, but implicit in this cycle is the process from errors (undefined variables/methods, etc.) to actual failures (expectations not met, etc.) Usually that first step from error to failure is a fairly trivial one (in this case, define a method called sent_messages with nothing in it to start, and run the test again), but it may not be depending on context.

Respondido el 04 de diciembre de 12 a las 04:12

Yes, your tests are initially supposed to fail (else, there's no work to be done), then you do the mínimo to make it pass. Every so often (I do every 20 tests when doing TDD), you refactor them.

Respondido el 04 de diciembre de 12 a las 04:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.