{"id":2441,"date":"2022-08-30T15:24:53","date_gmt":"2022-08-30T15:24:53","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/12\/meck-behaving-strangely-for-multiple-mocked-modules-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:24:53","modified_gmt":"2022-08-30T15:24:53","slug":"meck-behaving-strangely-for-multiple-mocked-modules-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/meck-behaving-strangely-for-multiple-mocked-modules-collection-of-common-programming-errors\/","title":{"rendered":"Meck behaving strangely for multiple mocked modules-Collection of common programming errors"},"content":{"rendered":"<p>I have following module<\/p>\n<pre><code>-module(bhavcopy_downloader).\n\n-export([download\/2]).\n\ndownload(From, SaveTo) -&gt;\n    {ok, {{Status, _}, _, Body}} = lhttpc:request(From, \"GET\", [], infinity),\n    case Status of \n        200 -&gt;  file:write(SaveTo, Body),\n            true;\n        _ -&gt; false\n    end.\n<\/code><\/pre>\n<p>And following tests for the above code<\/p>\n<pre><code>file_download_test_() -&gt; \n    {foreach,\n     fun() -&gt;\n            meck:new(lhttpc)\n            meck:new(file, [unstick])\n     end,\n     fun(_) -&gt;\n        meck:unload(file),\n            meck:unload(lhttpc) \n     end,\n      {\"saves the file at specified location\",\n        fun() -&gt;\n            meck:expect(lhttpc, request, 4, {ok, {{200, \"OK\"}, [], }}),\n            meck:expect(file, write_file, fun(Path, Data) -&gt; \n                                    ?assertEqual(Path, \"~\/Downloads\/data-downloader\/test.html\"), \n                                    ?assertEqual(Data, ) \n                            end),\n            ?assertEqual(true, bhavcopy_downloader:download(\"http:\/\/google.com\", \"~\/Downloads\/data-downloader\/test.html\")),\n            ?assert(meck:validate(file))\n        end}]\n\n    }.\n<\/code><\/pre>\n<p>When I run the tests I get following error (only part of the error pasted below for brevity). Looking at the error below, I am kind of feeling that file module is not being mocked (or the mock of file module being overridden when I set the other mock using <code>meck:new(lhttpc)<\/code>. What could be going wrong here?<\/p>\n<pre><code>=ERROR REPORT==== 16-Feb-2013::20:17:24 ===\n** Generic server file_meck terminating \n** Last message in was {'EXIT',,\n                    {compile_forms,\n                     {error,\n                      [{[],\n                        [{none,compile,\n                          {crash,beam_asm,\n                           {undef,\n                            [{file,get_cwd,[],[]},\n                             {filename,absname,1,\n                              [{file,\"filename.erl\"},{line,67}]},\n                             {compile,beam_asm,1,\n                              [{file,\"compile.erl\"},{line,1245}]},\n                             {compile,'-internal_comp\/4-anonymous-1-',2,\n                              [{file,\"compile.erl\"},{line,273}]},\n                             {compile,fold_comp,3,\n                              [{file,\"compile.erl\"},{line,291}]},\n                             {compile,internal_comp,4,\n                              [{file,\"compile.erl\"},{line,275}]},\n                             {compile,'-do_compile\/2-anonymous-0-',2,\n                              [{file,\"compile.erl\"},{line,152}]}]}}}]}],\n                      [{\"src\/lhttpc_types.hrl\",\n                        [{31,erl_lint,{new_builtin_type,{boolean,0}}},\n                         {31,erl_lint,{renamed_type,bool,boolean}}]}]}}}\n<\/code><\/pre>\n<ol>\n<li>\n<p>This is a catch 22 in Meck, caused by the fact that Meck uses the Erlang compiler, which in turns uses the <code>file<\/code> module. When Meck tries recompile the <code>file<\/code> module it needs the <code>file<\/code> module (through the compiler) and thus crashes.<\/p>\n<p>As of this moment, Meck doesn&#8217;t handle mocking the file module. Your best alternative is to wrap the <code>file<\/code> module calls in another module and mock this module instead.<\/p>\n<p>(<em>It is theoretically possible to fix this in Meck by using the internals of the compiler and the code server instead, for example <code>erlang:load_module\/2<\/code><\/em>, however this is quite tricky and needs to be designed and tested well)<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-12 20:47:54. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have following module -module(bhavcopy_downloader). -export([download\/2]). download(From, SaveTo) -&gt; {ok, {{Status, _}, _, Body}} = lhttpc:request(From, &#8220;GET&#8221;, [], infinity), case Status of 200 -&gt; file:write(SaveTo, Body), true; _ -&gt; false end. And following tests for the above code file_download_test_() -&gt; {foreach, fun() -&gt; meck:new(lhttpc) meck:new(file, [unstick]) end, fun(_) -&gt; meck:unload(file), meck:unload(lhttpc) end, {&#8220;saves the file [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2441","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2441"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2441\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}