<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Patrick Baselier</title>
    <description>The latest articles on DEV Community by Patrick Baselier (@patrick_baselier_6e6b230d).</description>
    <link>https://dev.to/patrick_baselier_6e6b230d</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F571417%2F96189a65-a946-4f40-958d-63a7c21fde3e.png</url>
      <title>DEV Community: Patrick Baselier</title>
      <link>https://dev.to/patrick_baselier_6e6b230d</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/patrick_baselier_6e6b230d"/>
    <language>en</language>
    <item>
      <title>You're not stubbing, stupid!</title>
      <dc:creator>Patrick Baselier</dc:creator>
      <pubDate>Fri, 17 Apr 2020 00:00:00 +0000</pubDate>
      <link>https://dev.to/kabisasoftware/you-re-not-stubbing-stupid-26h9</link>
      <guid>https://dev.to/kabisasoftware/you-re-not-stubbing-stupid-26h9</guid>
      <description>&lt;p&gt;Recently, in a &lt;a href="https://rubyonrails.org/"&gt;Ruby on Rails&lt;/a&gt; project, I was writing a &lt;a href="https://cucumber.netlify.app/docs/installation/ruby/"&gt;Cucumber&lt;/a&gt; scenario that was deleting a resource by having the user clicking a 'Destroy' button. Before the action was executed, the user had to confirm a message shown in a confirmation dialog. You may have seen this dozens of times when scaffolding a Rails application.&lt;br&gt;&lt;br&gt;
Oh and upon deleting, I also had to do a request to an external API (to be more precise: the use case was that of a user unsubscribing, so I had to send a &lt;code&gt;DELETE&lt;/code&gt; request to a &lt;a href="https://docs.mollie.com/reference/v2/subscriptions-api/cancel-subscription"&gt;Mollie API&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;So, "nothing new here", I thought. I knew about &lt;a href="https://github.com/bblimke/webmock"&gt;WebMock&lt;/a&gt;, since I wanted to stub the external API request and my test suite was set up to test &lt;a href="https://github.com/teamcapybara/capybara#setup"&gt;JavaScript&lt;/a&gt;, so I knew I could use the &lt;a href="https://www.rubydoc.info/github/jnicklas/capybara/Capybara%2FSession:accept_confirm"&gt;&lt;code&gt;accept_confirm&lt;/code&gt;&lt;/a&gt; method here that Capybara offers. &lt;/p&gt;

&lt;p&gt;Stubbing the request was defined in a support file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# features/support/webmock.rb&lt;/span&gt;

&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'webmock/cucumber'&lt;/span&gt;

&lt;span class="no"&gt;WebMock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;disable_net_connect!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;allow_localhost: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="no"&gt;Before&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;_scenario&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;stub_request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="ss"&gt;:delete&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="sr"&gt;%r{https://api.mollie.com/v2/customers/&lt;/span&gt;&lt;span class="se"&gt;\w&lt;/span&gt;&lt;span class="sr"&gt;+/subscriptions/&lt;/span&gt;&lt;span class="se"&gt;\w&lt;/span&gt;&lt;span class="sr"&gt;+}&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;body: &lt;/span&gt;&lt;span class="p"&gt;{}.&lt;/span&gt;&lt;span class="nf"&gt;to_json&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My step implementation looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# features/step_definitions/general_steps.rb&lt;/span&gt;

&lt;span class="no"&gt;When&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'I delete the resource'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;accept_confirm&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;click_on&lt;/span&gt; &lt;span class="s1"&gt;'Destroy'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The test failed! It was telling me that I should stub the DELETE request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  Real HTTP connections are disabled. Unregistered request: DELETE https://api.mollie.com/v2/customers/...

  You can stub this request with the following snippet:

  stub_request(:delete, "https://api.mollie.com/v2/customers/...").
    ...
    to_return(status: 200, body: "", headers: {})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wasn't I doing this?&lt;/p&gt;

&lt;p&gt;After many (!!!) hours of trying rewriting the stub, rubber ducking with colleagues, writing alternative scenarios, I decided to get rid of the confirmation dialog that was shown to the user. This way I didn't need the &lt;code&gt;accept_confirm&lt;/code&gt; and... tadaaa: it worked! My test was passing.&lt;/p&gt;

&lt;p&gt;My theory was that &lt;code&gt;accept_confirm&lt;/code&gt; executes in a different thread or something in which the stub is not defined (I know a theory can be proven wrong, but this one worked for me).&lt;br&gt;&lt;br&gt;
One way to work around this is, instead of using &lt;code&gt;accept_confirm&lt;/code&gt;, is 'overriding' the JavaScript's &lt;code&gt;confirm&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# features/support/my_world.rb&lt;/span&gt;
&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;MyWorld&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;click_on_and_confirm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;locator&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;link_or_button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:link_or_button&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;locator&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;evaluate_script&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'window.confirm = () =&amp;gt; true'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;link_or_button&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'data-confirm'&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;be_a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;link_or_button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;World&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;MyWorld&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# features/step_definitions/general_steps.rb&lt;/span&gt;

&lt;span class="no"&gt;When&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'I delete the resource'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;click_on_and_confirm&lt;/span&gt; &lt;span class="s1"&gt;'Destroy'&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way the DELETE request &lt;em&gt;is&lt;/em&gt; being stubbed, you can still show a nice confirmation dialog and your test will pass.  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Be aware that during the test, the confirmation dialog will not be shown anymore (due to the 'stubbed' &lt;code&gt;confirm&lt;/code&gt; function in JavaScript. Therefor, I added an assertion to check indirectly if the confirmation dialog will be shown (by using the &lt;code&gt;data-confirm&lt;/code&gt; attribute &lt;a href="https://guides.rubyonrails.org/working_with_javascript_in_rails.html#confirmations"&gt;added and used by Rails&lt;/a&gt;) when clicking the 'Destroy' link or button. &lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>cucumber</category>
    </item>
  </channel>
</rss>
