-1

I am trying to create a unit test for this error post subroutine shown below. This method takes in the error name and prints out the message which is a value located in each error. This is the code I have:

    use constant {
        # link included as a variable in this example
        ERROR_AED => {
        errorCode => 561,
        message => {"this is an error. "},
        tt => { template => 'disabled'},
        link => 'www.error-fix.com',
        },
};


sub error_post {

    my($error) = @_;
    printf ($error->{ message });    
}

error_post(ERROR_AED);

This was my approach I'm pretty sure it's wrong I was attempting to verify the input values, or more generally verify that it was an error that was passed into the error_post method.

#verifying input values 

sub test_error_post {
    ok( defined $error, 'Should have an input value' );  # check that it's a constant
    ok($error->isa(constant) , 'Error should be of type constant');
    ok($error->errorCode), 'Should contain key errorCode');
    ok($error->message), 'Should contain key message'); 
    ok($error->tt), 'Should contain key tt');
    ok($error->wiki_page) 'Should contain key wiki page');      
}

I know this is probably a long way off.

halfer
  • 19,824
  • 17
  • 99
  • 186
Paul Russell
  • 179
  • 10
  • 1
    You have stray parentheses on the last four lines. Edit it so that at least you have something that compiles. – mob Mar 14 '16 at 19:02
  • 2
    You're also missing a semi-colon after `use constant { ... }` and you forgot the quotes around `www.error-fix.com`. Please don't invent code that you're not actually using and type it directly into the question box; create a [mcve] and copy-paste it verbatim instead. (This is just general advice, by the way; I can see that you're asking about the right approach and not about compilation errors.) – ThisSuitIsBlackNot Mar 14 '16 at 19:05
  • Also, the way things are configured in your tests, you're attempting to call methods on an `$error` object, instead of checking the values of hash keys. Have you even attempted to run this code? – stevieb Mar 14 '16 at 19:19
  • Addressing your actual question: what are you trying to test? It seems like you're trying to check for invalid input to `error_post()`, using `ok()` like `assert()` in C. To unit test a function, usually you pass a known input and check that the output matches what you expect, e.g. `my $error = { message => 'foo' }; is( error_post($error), $error->{message}, 'returns message' );` – ThisSuitIsBlackNot Mar 14 '16 at 19:32

1 Answers1

1

I didn't have much time, but here's some tests somewhat along the lines on how I'd write them. However, tests should live in their own files, have a *.t extension, and live within their own directory. They shouldn't be inline with the code you're writing.

use warnings;
use strict;

use Test::More;

use constant {

    ERROR_AED => {
        errorCode => 561,
        message => "this is an error.\n",
        tt => { template => 'disabled'},
        link => 'www.error-fix.com',
    },
};

my $error = ERROR_AED;

is (ref $error, 'HASH', 'ERROR_AED is a hashref');

is (defined $error->{errorCode}, 1, 'ERROR_AED contains an errorCode key');
is ($error->{errorCode}, 561, 'ERROR_AED errorCode is correct');

is (defined $error->{message}, 1, 'ERROR_AED contains key message');
like ($error->{message}, qr/this is an error/, 'ERROR_AED msg output is ok');

is (defined $error->{tt}, 1, 'ERROR_AED contains key tt');
is (ref $error->{tt}, 'HASH', 'ERROR_AED tt is a hashref');
is (defined $error->{tt}{template}, 1, 'ERROR_AED tt href contains template key');
is ($error->{tt}{template}, 'disabled', 'ERROR_AED tt template is ok');

is (defined $error->{link}, 1, 'ERROR_AED has a link key');
is ($error->{link}, 'www.error-fix.com', 'ERROR_AED link is ok');

done_testing();
stevieb
  • 9,065
  • 3
  • 26
  • 36