erlangmockingtddstubmeck

How to mock objects in Erlang using Meck?


Okay, I'm using Meck and I'm lost. My first language (that I've been writing for about 7 months) is Ruby, so I can't seem to wrap my brain around Meck mocking yet. I do get Ruby mocking though. Hoping someone can help me. Also, I've only been writing Erlang for a week.

Updated Code (but mocking still isn't working)...

I have a Erlang console_io prompter module that looks like this:

    -module(prompter).
    -export([prompt/1, guess/0]).

    prompt(Message) ->
      console_io:gets(Message).

    gets() ->
      {_, [Input]} = io:fread("Enter:  ", "~s"),
      Input.

    guess() ->
      Guess_Input = gets(),
      Guess_List = convert_guess_to_list(Guess_Input).

    convert_guess_to_list(Guess_Input) ->
      re:split(Guess_Input, "", [{return, list}, trim]).

My test now looks like this:

    -module(prompter_test).
    -include_lib("eunit/include/eunit.hrl").

    guess_1_test() ->
      meck:new(prompter),
      meck:expect(prompter, gets, fun() -> "aaaa" end),
      ?assertEqual(prompter:guess(), ["a","a","a","a"]),
      ?assert(meck:validate(prompter)),
      meck:unload(prompter).

The error I'm getting is this:

    Eshell V5.9.3.1  (abort with ^G)
    1> prompter_test: guess_1_test (module 'prompter_test')...*failed*
    in function prompter:guess/0
      called as guess()
    in call from prompter_test:guess_1_test/0 (test/prompter_test.erl, line 10)
    in call from prompter_test:guess_1_test/0
    **error:undef

I want to mock (stub?) the gets function in my test so that gets will return "aaaa" and then when I assert on get_guess() it should equal ["a", "a", "a", "a"].

How do I do this?


Solution

  • There are two problems: