josephwilk / amrita

A polite, well mannered and thoroughly upstanding testing framework for Elixir
201 stars 28 forks source link

provided fails to match with multiple test facts #94

Open smpallen99 opened 10 years ago

smpallen99 commented 10 years ago

I have a short example with test facts. Both facts pass by themselves (one or the other is commented out), but fail when both are present. Here is the output:

➜  mock git:(master) ✗ mix test
Elixir.MockTest
  sends a syncro message
  sends by cabinet
Failures:
  1) test sends by cabinet (MockTest)
     ** (Amrita.MockError) Expected:
     Mdse.HbManager.send_udp(nil,nil,32780,1) to be called but was called 0 times.
       Actuals calls:
         * Mdse.HbManager.send_udp(nil,nil,32780,1)
     stacktrace:
       lib/amrita/message.ex:16: Amrita.Message.mock_fail/1
       test/mock_test.exs:6: MockTest."test sends by cabinet"/1
Finished in 0.1 seconds (0.07s on load, 0.05s on tests)
2 facts, 1 failures

Here is my test code: mock_test.ex

Code.require_file "../test_helper.exs", __ENV__.file
defmodule MockTest do
  use Amrita.Sweet
  it "sends by cabinet" do
    provided [Mdse.HbManager.send_udp(nil, nil, 32780, 1) |> true] do
      Mdse.HbManager.send(nil, 32780, 1) |> :ok
    end
  end
  it "sends a syncro message" do
    msg = <<1::32, 2::32, 3::32, 0::32, 6::32>>
    provided [Mdse.HbManager.send_udp(_, {}, 0, _) |> true] do
      Mdse.HbManager.handle_info({:udp, 0, {}, 0, msg}, nil) |> {:noreply, nil}
    end
  end
end

And the source...

supervisor.ex

defmodule Mock.Supervisor do
  use Supervisor.Behaviour
  def start_link do
    :supervisor.start_link(__MODULE__, [])
  end
  def init([]) do
    children = [ worker(Mdse.HbManager, []) ]
    supervise(children, strategy: :one_for_one)
  end
end

hb_manager.ex

defmodule Mdse.HbManager do
  use GenServer.Behaviour
  def start_link() do
    :gen_server.start_link({ :local, :HbManager }, __MODULE__, 0, [])
  end
  def send(ip_or_cabinet, port, message) do
    :gen_server.cast :HbManager, {:send, ip_or_cabinet, port, message }
  end
  def init(_) do
    {:ok, nil}
  end
  def handle_cast({:send, ip, port, message}, state) do
    Mdse.HbManager.send_udp state , ip, port, message
    {:noreply, state}
  end
  def handle_info({:udp, _sock, ip, port, message}, state) do
    Mdse.HbManager.send_udp(state, ip, port, message)
    {:noreply, state}
  end
  ###########
  # Helpers
  def send_udp(sock, ip, port, message) do
    IO.puts "send_udp #{inspect sock} #{inspect ip}:#{port} - #{inspect message}"
    #:gen_udp.send sock, ip, port, message
  end
end
josephwilk commented 10 years ago

Thanks for the detailed report, I'll investigate.

smpallen99 commented 10 years ago

Any update on this issue? If you do not have time to investigate, perhaps you can point out a few things I can investigate myself. This is blocking me from any mocked testing in my application.

Thanks, Steve

josephwilk commented 10 years ago

@smpallen99 I'll take a look to day and at least have some guidance as to the problem if I cannot fix it in the time I have.

Thanks