Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MR::Go::Admin::Test

 MR::Go::Admin::Test

Vadim Pushtaev

January 23, 2017
Tweet

More Decks by Vadim Pushtaev

Other Decks in Programming

Transcript

  1. package MR::Go::Admin::User::Test; sub setup : Test(setup) { my ($self) =

    @_; $self->{user} = create_user(); } sub test_delete : Test(2) { my ($self) = @_; $self->{user}->delete(); # ... } Test::Class
  2. Именование методов delete → delete delete → delete__already_deleted _delete →

    delete delete → test_delete delete → test_delete__already_deleted _delete → test__delete__already_deleted Двойное подчеркивание
  3. https://goo.gl/tBIRwZ sub _set_status { my ($self, $status) = @_; $self->_status($status);

    $self->_history->add($status); $self->_log('...'); } sub set_status_processing { return shift->_set_status('processing'); } sub set_status_processed { return shift->_set_status('processing'); } Тестирование приватных методов
  4. package MR::Go::Admin::UserFactory; use base qw(DBIx::Class::Factory); __PACKAGE__->resultset(MR::Go::Admin:: Schema->resultset('User')); __PACKAGE__->fields({ name =>

    __PACKAGE__->seq(sub {'User #' . shift}), city => __PACKAGE__->related_factory('MR::Go::Admin::CityFactory'), status => 'new', }); DBIx::Class::Factory
  5. package MR::Go::Admin::UserFactory; use base qw(DBIx::Class::Factory); __PACKAGE__->resultset(MR::Go::Admin:: Schema->resultset('User')); __PACKAGE__->fields({ name =>

    __PACKAGE__->seq(sub {'User #' . shift}), city => __PACKAGE__->related_factory('MR::Go::Admin::CityFactory'), status => 'new', }); https://goo.gl/tBIRwZ DBIx::Class::Factory
  6. Фабрика в деле sub set_status : Test(1) { my ($self)

    = @_; my $task = MR::Admin::Go::Test::Factory::Task->create(); $task->set_status('TEST_NEW_STATUS'); is($task->status, 'TEST_NEW_STATUS', 'status'); return; }
  7. Моки с магией local *MR::Go::Admin::SMS::_send = sub { $sent++ };

    my $mock = Test::MockModule->new('MR::Go::Admin::SMS'); my $sent = 0; $mock->mock(_send => sub { $sent = 1 });
  8. Моки без магии has _logger => (default => sub {

    '...' }); User->new({_logger => $fake_logger}); User->new({_message_size_limit => 42}); package MR::Go::Admin::User::Test::_FakeUser; MR::Go::Admin::User::Test::_FakeUser->new(); bless $user, 'MR::Go::Admin::User::Test::_FakeUser';