<?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: Yassu</title>
    <description>The latest articles on DEV Community by Yassu (@seteen).</description>
    <link>https://dev.to/seteen</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%2F84822%2F785ed8a0-dfae-41e4-8601-7c0d998e5f91.png</url>
      <title>DEV Community: Yassu</title>
      <link>https://dev.to/seteen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/seteen"/>
    <language>en</language>
    <item>
      <title>Make your RSpec codes HALF. Introduction of RSpecZ extending RSpec for reviewers and developers.</title>
      <dc:creator>Yassu</dc:creator>
      <pubDate>Mon, 16 Jul 2018 15:28:02 +0000</pubDate>
      <link>https://dev.to/seteen/make-your-rspec-codes-half-introduce-rspexz-extending-rspec-for-reviewers-and-developers-1fg6</link>
      <guid>https://dev.to/seteen/make-your-rspec-codes-half-introduce-rspexz-extending-rspec-for-reviewers-and-developers-1fg6</guid>
      <description>&lt;p&gt;I talked at Omotesando.rb(Ruby meetup in Japan) on the other day.&lt;br&gt;
Surprisingly, Matz attended on that meetup.&lt;/p&gt;

&lt;p&gt;At there, Matz give a comment to RSpecZ, a gem for RSpec. &lt;br&gt;
"You should make a Pull Request in RSpec repository."&lt;/p&gt;

&lt;p&gt;I didn't make Pull Request yet.&lt;br&gt;
But I noticed that a lot of people are interesting to RSpecZ.&lt;br&gt;
So, I introduce RSpecZ here first.&lt;/p&gt;
&lt;h1&gt;
  
  
  What is RSpecZ?
&lt;/h1&gt;

&lt;p&gt;RSpec has a big problem.&lt;/p&gt;

&lt;p&gt;RSpec codes are Long (means Hard to review).&lt;/p&gt;

&lt;p&gt;RSpecZ is trying to solve this problem.&lt;br&gt;
It has methods of extending &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;context&lt;/code&gt; and &lt;code&gt;subject&lt;/code&gt; now.&lt;br&gt;
I want RSpecZ to be a Guide of RSpec in the future so I will make &lt;code&gt;generators&lt;/code&gt;, &lt;code&gt;code analyzer and formatter&lt;/code&gt; and something like that.&lt;/p&gt;

&lt;p&gt;I show you RSpecZ effectiveness with Image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F0omvjmh853t9rf7b11pp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F0omvjmh853t9rf7b11pp.png" alt="001"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This image is created by applying RSpecZ to a rspec code from a project I participated.&lt;/p&gt;

&lt;p&gt;I will tell you how this can be happened.&lt;/p&gt;
&lt;h1&gt;
  
  
  Basic Features
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Extend context
&lt;/h2&gt;

&lt;p&gt;RSpec's &lt;code&gt;context&lt;/code&gt; method is flexible and versatilie.&lt;br&gt;
In the other words, it tends to be redundant and hard to understand.&lt;/p&gt;

&lt;p&gt;For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;context 'When phone_number is 090-1234-5678' do
  let(:phone_number) { '090-1234-5678' }
  it { expect(phone.save).to be_truthy }
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;( &lt;code&gt;090-1234-5678&lt;/code&gt; is valid phone number in Japan.)&lt;/p&gt;

&lt;p&gt;This &lt;code&gt;context&lt;/code&gt; is redundant. Because &lt;code&gt;context&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; show same thing.&lt;/p&gt;

&lt;p&gt;But this will be happened very often when we use RSpec.&lt;/p&gt;

&lt;p&gt;And one more problem.&lt;br&gt;
RSpec's &lt;code&gt;context&lt;/code&gt; has no meaning by itself. &lt;code&gt;context&lt;/code&gt; first argument show state of the context.&lt;br&gt;
Without first argument, we cannot know what kind of context it make.&lt;/p&gt;

&lt;p&gt;The above code means that set valid phone number to &lt;code&gt;phone_number&lt;/code&gt; variable(let).&lt;br&gt;
RSpecZ make this code simple.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set_valid(:phone_number, '090-1234-5678') do
  it { expect(phone.save).to be_truthy }
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;set_valid&lt;/code&gt; method combine &lt;code&gt;context&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt;.&lt;br&gt;
And method name can say &lt;code&gt;valid&lt;/code&gt; value will be put to &lt;code&gt;phone_number&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Reviewer can know &lt;code&gt;valid&lt;/code&gt; specs will be written in the block when he read the method name.&lt;br&gt;
And lines of code become short.&lt;/p&gt;

&lt;p&gt;With RSpecZ you can write multiple valid cases Very Easily.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set_valids(:phone_number, '090-1234-5678', '080-9566-8466', '070-4567-8901', '09045678945' ) do
  it { expect(phone.save).to be_truthy }
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same code with RSpec will be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;%w[090-1234-5678 080-9566-8466 070-4567-8901 09045678945].each do |phone_number|
  context "When phone_number is #{phone_number}" do
    let(:phone_number) { phone_number }
    it { expect(phone.save).to be_truthy }
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can know how easy to review RSpecZ codes with comparing these codes.&lt;/p&gt;

&lt;p&gt;RSpecZ has not only &lt;code&gt;set_valid&lt;/code&gt; but also &lt;code&gt;set_invalid&lt;/code&gt; for case of &lt;code&gt;invalid&lt;/code&gt; value set.&lt;/p&gt;

&lt;p&gt;Choose correct method to help reviewer review codes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Extend let
&lt;/h2&gt;

&lt;p&gt;When you write RSpec, you may write this kind of codes,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let(:name) { 'test-name' }
let(:phone_number) { '090-1234-5678' }
let(:address) { 'test-address' }
let(:params) { {
  name: name,
  phone_number: phone_number,
  address: address
} }

subject { get '/api/test', params: params }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;code&gt;let&lt;/code&gt; s can help you to change a part of context from default value.&lt;br&gt;
You can write flexible specs.&lt;/p&gt;

&lt;p&gt;However, this codes are also redundant.&lt;/p&gt;

&lt;p&gt;RSpecZ has &lt;code&gt;create_params&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let(:name) { 'test-name' }
let(:phone_number) { '090-1234-5678' }
let(:address) { 'test-address' }
create_params :name, :phone_number, :address

subject { get '/api/test', params: params }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code has same meaning.&lt;br&gt;
&lt;code&gt;create_params&lt;/code&gt; define &lt;code&gt;params&lt;/code&gt; &lt;code&gt;let&lt;/code&gt; with using defined &lt;code&gt;let&lt;/code&gt; s.&lt;/p&gt;

&lt;p&gt;Actually, &lt;code&gt;create_params&lt;/code&gt; define &lt;code&gt;let&lt;/code&gt; with &lt;code&gt;test-xxx&lt;/code&gt; characters if the param is not defined yet.&lt;br&gt;
So, below code also has same meaning.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let(:phone_number) { '090-1234-5678' }
create_params :name, :phone_number, :address

subject { get '/api/test', params: params }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I will implement more useful &lt;code&gt;let&lt;/code&gt; methods in the future.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other features
&lt;/h2&gt;

&lt;p&gt;RSpecZ has some other useful methods.&lt;br&gt;
Please check github repository if you are interested.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/RSpecZ/RSpecZ" rel="noopener noreferrer"&gt;https://github.com/RSpecZ/RSpecZ&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Mesurement effectiveness of RSpecZ
&lt;/h1&gt;

&lt;p&gt;I check the effectiveness of RSpecZ with compare projects.&lt;br&gt;
One is a rails project without RSpecZ (I participate before) and the other one is a rails project with RSpecZ (I currently participate).&lt;/p&gt;

&lt;h2&gt;
  
  
  Premises
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;These projects are completely different. They has different business logics.&lt;/li&gt;
&lt;li&gt;I developed code base of both of projects. ( Basic code styles are same)&lt;/li&gt;
&lt;li&gt;Project with RSpecZ is just started(3 months) so codes are less.&lt;/li&gt;
&lt;li&gt;We use other features in project with RSpecZ which RSpecZ not yet have. So, not only RSpecZ feature make codes shorter.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Results
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Lines of RSpec&lt;/th&gt;
&lt;th&gt;Num of tests&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Project with RSpecZ&lt;/td&gt;
&lt;td&gt;2183&lt;/td&gt;
&lt;td&gt;1223&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Project without RSpecZ&lt;/td&gt;
&lt;td&gt;20023&lt;/td&gt;
&lt;td&gt;5143&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Lines of RSpec: Total lines of &lt;code&gt;request_spec&lt;/code&gt; and &lt;code&gt;support&lt;/code&gt; files.&lt;br&gt;
Num of tests: Number of examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;RSpecZ can make RSpec codes HALF.&lt;br&gt;
(CANNOT compare just RSpecZ features because of Premises)&lt;/p&gt;

&lt;p&gt;This is may not accurate but we can know the power of RSpecZ from result.&lt;/p&gt;

&lt;h1&gt;
  
  
  At last
&lt;/h1&gt;

&lt;p&gt;This article introduce RSpecZ features.&lt;/p&gt;

&lt;p&gt;RSpecZ is just started a few months before.&lt;br&gt;
We have very few commiters.&lt;/p&gt;

&lt;p&gt;Please give comments if you are interested.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rspec</category>
      <category>rails</category>
      <category>gem</category>
    </item>
  </channel>
</rss>
