{"id":1603,"date":"2015-10-20T05:21:04","date_gmt":"2015-10-20T05:21:04","guid":{"rendered":"https:\/\/intelligentbee.com\/blog\/?p=1603"},"modified":"2024-10-25T09:19:44","modified_gmt":"2024-10-25T09:19:44","slug":"how-to-fix-common-errors-when-testing-in-rspec","status":"publish","type":"post","link":"https:\/\/intelligentbee.com\/blog\/how-to-fix-common-errors-when-testing-in-rspec\/","title":{"rendered":"How to Fix Common Errors When Testing in RSpec"},"content":{"rendered":"<p>If you are a <a title=\"What Is the Difference between QA and QC\/Software Testing\" href=\"https:\/\/intelligentbee.com\/blog\/what-is-the-difference-between-qa-and-qcsoftware-testing\/\" target=\"_blank\" rel=\"noopener\">Software Test Engineer or Quality Control Engineer<\/a> and you want to automate your API call tests, then you should try RSpec (Ruby&#8217;s testing framework). I didn\u2019t exactly chose it (the QC team was already using it), but I tend to believe that I would have picked it in the future for my own tests because when it comes to the installation of the program, the process is not that complicated at all.<\/p>\n<h2>How to Fix Common Errors When Testing in RSpec<\/h2>\n<p>Actually, let me show you how little you need to do in order to start writing your own tests:<\/p>\n<pre class=\"lang:default decode:true \">gem install rspec\r\nrspec --init<\/pre>\n<p>Yep, that\u2019s all. Now you can automate your tests and run them with the following command:<\/p>\n<pre class=\"lang:default decode:true \">rspec\u00a0your_test_suite.rb<\/pre>\n<p>I\u00a0will now show you some common error messages that I&#8217;ve encountered, so that you can avoid them during your work. These errors are caused by very small mistakes, but usually in the rush of delivering quality we skip some things or words.<\/p>\n<p>&nbsp;<\/p>\n<p><code>1. syntax error, unexpected keyword_end, expecting end-of-input (SyntaxError)<\/code><\/p>\n<p>Let\u2019s take a look at the following examples:<\/p>\n<pre class=\"lang:ruby decode:true\">describe 'Test Suite' \r\n   it 'Validate successful response' do\r\n      response = RestClient.get('www.intelligentbee.com')\r\n      expect(response.code).to eq(200)\r\n   end\r\nend<\/pre>\n<pre class=\"lang:ruby decode:true\">describe 'Test Suite' do\r\n   it 'Validate successful response' \r\n      response = RestClient.get('www.intelligentbee.com')\r\n      expect(response.code).to eq(200)\r\n   end\r\nend<\/pre>\n<p>So, if you get the above error, you most probably forgot to put a <strong>\u2018do\u2019<\/strong> after <strong>\u2018describe\u2019<\/strong> or <strong>\u2018it\u2019<\/strong> methods.<\/p>\n<p>&nbsp;<\/p>\n<p><code>2. syntax error, unexpected end-of-input, expecting keyword_end (SyntaxError)<\/code><\/p>\n<p>I will use the same example again:<\/p>\n<pre class=\"lang:ruby decode:true\">describe 'Test Suite' do\r\n   it 'Validate successful response' do\r\n      response = RestClient.get('www.intelligentbee.com')\r\n      expect(response.code).to eq(200)\r\n   \r\nend<\/pre>\n<p>What is wrong with this? Well, I missed an <strong>\u2018end\u2019<\/strong>. I\u2019ll take it you can figure out by yourself where it should be placed.<\/p>\n<p>&nbsp;<\/p>\n<p><code>3. JSON::ParserError: 757: unexpected token <\/code><\/p>\n<p>Take a look:<\/p>\n<pre class=\"lang:ruby decode:true\">describe 'Test Suite' do\r\n   it 'Validate successful response' do\r\n      response = RestClient.get('www.intelligentbee.com')\r\n      parsed_response = JSON.parse(response)\r\n      expect(parsed_response['message']).to eq \"Some message\"\r\n   end\r\nend<\/pre>\n<p>Supposedly, sometimes you will get as an answer a JSON and you will want to parse it for better tests. You will get the above error if the answer is not a JSON and the parser can\u2019t find there what it expects.<\/p>\n<p>I hope you will find this short guide useful, I plan to continue writing about common errors that we may encounter while using RSpec.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are a Software Test Engineer or Quality Control Engineer and you want to automate your API call tests, [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":1616,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[84],"tags":[202,213,214,215,231],"yst_prominent_words":[353,446,594,794,1237,2330],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/1603"}],"collection":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/comments?post=1603"}],"version-history":[{"count":3,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/1603\/revisions"}],"predecessor-version":[{"id":133285,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/1603\/revisions\/133285"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media\/1616"}],"wp:attachment":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media?parent=1603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/categories?post=1603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/tags?post=1603"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=1603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}