You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
Hi. I'd like to be able to test my cogs using mix test. I looked at Viviani but did not find any tests for the Cogs.
I basically want to make sure a Cog properly performs its job and if I set a custom parser for a command, the right arguments are sent to the function. I would also like a way to inject mocks. Either by adding that to the message variable available in the Cog, or preferably passing options to the command function (see hello below).
It could look like this:
defmoduleMyApp.Commands.HellodouseAlchemy.CogsCogs.set_parser(:hello,&List.wrap/1)Cogs.defhello(args,opts\\[])doservice=Keyword.get(opts,:service,MyApp.HelloService)caseservice.handle(args)do{:ok,response}->Cogs.say(response){:error,_}->Cogs.say("An error occurred.")endendenddefmoduleMyApp.Commands.HelloTestdouseAlchemy.TestCase,async: falseimportMoxsetup:set_mox_from_contextsetup:verify_on_exit!@service_mock__MODULE__.Hello.ServiceMockdefmock(@service_mock,for: Hello.Behaviour)test"command hello responds with Hi",%{cogs: cogs}doexpect(@service_mock,:handle,fnargs->assertargs=="my name is Foo"{:ok,"Hi Foo!"}endcog_options = [service: @service_mock]
response =handle_message("!hello my name is Foo",cog_options)
assert response.text=="Hi Foo!"endend
Is there anything I can do for now for testing my Cogs without having the source code of Alchemy changed?
The text was updated successfully, but these errors were encountered:
I think the problem with extra parameters is that you'd need some way to know which commands do or don't have extra parameters, and how to supply them. You could modify the library to always take an extra argument for options, allowing you to call it by filling that parameter in for testing.
My motto for testing commands would be to try and isolate the necessary logic outside of the command itself, which would act as glue for the most part. So you'd be able to test the parsing function in isolation, the logic for choosing what text to return etc.
This requires a bit more work, but is a bit plainer to understand. For integration, it makes more sense IMO to test how these things interact with the actual API, since most problems will be around things like not calling the right effectful function, or things like that.
Thanks @cronokirby. Yes, the service will definitely be tested out in isolation. It is in fact in a different child app in the umbrella. Here, I just wanted to make sure that I have some tests that covers the glue between Alchemy and my service, to have some peace of mind when in the future I upgrade elixir, alchemy or other parts of the code base.
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Hi. I'd like to be able to test my cogs using
mix test
. I looked atViviani
but did not find any tests for the Cogs.I basically want to make sure a Cog properly performs its job and if I set a custom parser for a command, the right arguments are sent to the function. I would also like a way to inject mocks. Either by adding that to the
message
variable available in the Cog, or preferably passing options to the command function (seehello
below).It could look like this:
Is there anything I can do for now for testing my Cogs without having the source code of Alchemy changed?
The text was updated successfully, but these errors were encountered: