Does ActiveRecord really make Ruby on Rails code difficult to test?

I spent most of my time in statically typed languages ​​(primarily in C #). I have poor experience with the Active Record pattern and unit testing due to static methods and a combination of entities and data access code.

Since the Ruby community is probably the majority of community-driven tests, and Rails ActiveRecord seems popular, there should be some way to combine TDD and ActiveRecord code in Ruby on Rails.

I would suggest that the problem disappears in dynamic languages ​​anyway, but I don't see how to do it. So what is the trick?

+3
source share
2 answers

The problem is that the lights are terrible. Some time ago I read an article called The Lie , and it opened my eyes. This article focuses on the Machinist , which I used and loved, but currently prefer factory_girl . Here is a basic example from their README:

You define a Factory with a sufficient default value to pass the tests:

# This will guess the User class
Factory.define :user do |u|
  u.first_name 'John'
  u.last_name  'Doe'
  u.admin false
end

And then in your tests you can override or add values ​​as needed (a contrived RSpec example)

describe User, "#is_admin?" do
  before(:each) do
    @user = Factory(:user, :admin => true, :email => 'jdoe@email.com')
  end

  it "should be true"
    @user.is_admin?.should be_true
  end
end

@user , , ActiveRecord, () . . , . , (save false, , ).

, , .

+3

, . ActiveRecord . , Rails.

Rails , TDD Rails. RSpec ( , mocking screencast) Shoulda.

+2

Source: https://habr.com/ru/post/1741679/


All Articles