Reusing functional tests with rails 4

13 views
Skip to first unread message

João Pereira

unread,
Jul 19, 2013, 7:43:48 AM7/19/13
to rubyonra...@googlegroups.com

With rails 4, I have this test for one controller:

class Merchant::RegistrationsControllerTest < ActionController::TestCase
  test "should create new registration" do
    user = FactoryGirl.build(:merchant_user)
    #rest of the test using user as input for the controler
   end

  test "should not create new registration" do
    user = FactoryGirl.build(:merchant_user)
    #rest of the test using user as input for the controler
  end
end

And this other test for other controller:

class FrontStore::RegistrationsControllerTest < ActionController::TestCase
  test "should create new registration" do
    user = FactoryGirl.build(:front_store_user)
    #rest of the test using user as input for the controler
   end

  test "should not create new registration" do
    user = FactoryGirl.build(:front_store_user)
    #rest of the test using user as input for the controler
  end
end

As you can see, Is violation of the DRY principle, because my tests are the same, but using a different fixture.

As a note, both controllers inherits from the ApplicationRegistrationsController, that implements all the behaviour to both Merchant::RegistrationsController and FrontStore::RegistrationsController.

I want to run both test for each of the role (Merchant or FrontStore) but not repeating the test code.

My question is, how best design the test so they are reusable in this case?

Robert Walker

unread,
Jul 19, 2013, 8:47:58 AM7/19/13
to rubyonra...@googlegroups.com
J. mp wrote in post #1115983:
> I want to run both test for each of the role (Merchant or FrontStore)
> but
> not repeating the test code.
>
> My question is, how best design the test so they are reusable in this
> case?

IMHO you shouldn't. Test code should be DAMP not DRY:

http://stackoverflow.com/questions/6453235/what-does-damp-not-dry-mean-when-talking-about-unit-tests?lq=1

--
Posted via http://www.ruby-forum.com/.
Reply all
Reply to author
Forward
0 new messages