# RSpec

**URL:** https://www.ruby-forum.com/c/testing/rspec/25.md

[Latest](https://www.ruby-forum.com/latest.md) · [Categories](https://www.ruby-forum.com/categories.md) · [Tags](https://www.ruby-forum.com/tags.md)

---

## [Custom fail message on an around hook with a Timeout exception](https://www.ruby-forum.com/t/custom-fail-message-on-an-around-hook-with-a-timeout-exception/267563)

<div class="topic-metadata">

**Author:** [@gditrick](https://www.ruby-forum.com/u/gditrick)\
**Replies:** 0\
**Last updated:** [March 13, 2025, 3:35am UTC](https://www.ruby-forum.com/t/custom-fail-message-on-an-around-hook-with-a-timeout-exception/267563 "2025-03-13T03:35:48Z")

</div>

doing this which works to make sure that no infinite loop happens: RSpec.configure do |config| config.around(:example, finite: true) do |example| Timeout.timeout(wait\_time) do example.run end end end My desired e…

---

## [Testing an API I'm creating, need test server to be running](https://www.ruby-forum.com/t/testing-an-api-im-creating-need-test-server-to-be-running/215749)

<div class="topic-metadata">

**Author:** [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Replies:** 7\
**Last updated:** [June 18, 2013, 6:07pm UTC](https://www.ruby-forum.com/t/testing-an-api-im-creating-need-test-server-to-be-running/215749 "2013-06-18T18:07:49Z")

</div>

Hi, As I am writing a brand new API, I’d like to test it’s response. Obviously, controller tests are great and work like a charm but as I expose data which are not really resources, I’ve to make some DIY to provide r…

---

## [Generic example or template for RSpec request tests for autogenerated scaffold code](https://www.ruby-forum.com/t/generic-example-or-template-for-rspec-request-tests-for-autogenerated-scaffold-code/259315)

<div class="topic-metadata">

**Author:** [@EthanS](https://www.ruby-forum.com/u/EthanS)\
**Replies:** 1\
**Last updated:** [November 4, 2021, 12:43pm UTC](https://www.ruby-forum.com/t/generic-example-or-template-for-rspec-request-tests-for-autogenerated-scaffold-code/259315 "2021-11-04T12:43:54Z")

</div>

I am just learning RSpec. I generated resources using rails generate scaffold … I am now creating RSpec tests for the generated code. This is a generic need. Does anyone have an example or template for testing basic …

---

## [Assert Method is Used? (ActiveRecord Scopes)](https://www.ruby-forum.com/t/assert-method-is-used-activerecord-scopes/261854)

<div class="topic-metadata">

**Author:** [@mball](https://www.ruby-forum.com/u/mball)\
**Replies:** 0\
**Last updated:** [September 21, 2021, 9:19am UTC](https://www.ruby-forum.com/t/assert-method-is-used-activerecord-scopes/261854 "2021-09-21T09:19:52Z")

</div>

Long time lurker… I fully admit: This is kind of an exceptional use case for rspec. I’m teaching a course which uses Rails, and as such I’ve got some tricky things I’d like to test for. How can I expect that a method ca…

---

## [Write model rspec for my application](https://www.ruby-forum.com/t/write-model-rspec-for-my-application/260353)

<div class="topic-metadata">

**Author:** [@AshwiniRane](https://www.ruby-forum.com/u/AshwiniRane)\
**Replies:** 0\
**Last updated:** [October 11, 2020, 1:21pm UTC](https://www.ruby-forum.com/t/write-model-rspec-for-my-application/260353 "2020-10-11T13:21:05Z")

</div>

I am a beginner in ruby and rails and struggling to write rspec code for my model methods as given below: Please help me write rspec implementation and test examples for my methods for me to have a basic understanding o…

---

## [Question about test FAIL](https://www.ruby-forum.com/t/question-about-test-fail/260118)

<div class="topic-metadata">

**Author:** [@Beto](https://www.ruby-forum.com/u/Beto)\
**Replies:** 2\
**Last updated:** [August 15, 2020, 10:42am UTC](https://www.ruby-forum.com/t/question-about-test-fail/260118 "2020-08-15T10:42:44Z")

</div>

Hi folks! I am a beginner stundent in Ruby and Rspec and I have a question about a test using be\_within Please, take a look the follow screen below. The error above shou not be occur. Right? Thanks in advance! …

---

## [Testing rescue (tdd with rspec)](https://www.ruby-forum.com/t/testing-rescue-tdd-with-rspec/253118)

<div class="topic-metadata">

**Author:** [@juannodeb](https://www.ruby-forum.com/u/juannodeb)\
**Replies:** 1\
**Last updated:** [February 11, 2020, 6:50pm UTC](https://www.ruby-forum.com/t/testing-rescue-tdd-with-rspec/253118 "2020-02-11T18:50:06Z")

</div>

Hello. I’m getting into TDD, I’m using rspec and I’m having a hard time trying to test rescues(exceptions). Can anyone please advice me on how to test exceptions with RSpec 3.8? Thanks in advance

---

## [How to sort div elements in Ruby? (Web Automation, Cucumber, Watir)](https://www.ruby-forum.com/t/how-to-sort-div-elements-in-ruby-web-automation-cucumber-watir/253428)

<div class="topic-metadata">

**Author:** [@Ruby\_Newbie](https://www.ruby-forum.com/u/Ruby_Newbie)\
**Replies:** 0\
**Last updated:** [December 6, 2019, 7:08pm UTC](https://www.ruby-forum.com/t/how-to-sort-div-elements-in-ruby-web-automation-cucumber-watir/253428 "2019-12-06T19:08:22Z")

</div>

I have the following problem - I don’t know how I can sort div elements, which have the same class, same id’s, same name, etc. The only thing that differentiates them are the tags inside them. I am currently learning we…

---

## [Can Rspec take in a variable from the Command Line and use that variable in all tests?](https://www.ruby-forum.com/t/can-rspec-take-in-a-variable-from-the-command-line-and-use-that-variable-in-all-tests/252887)

<div class="topic-metadata">

**Author:** [@GeorgeTWalters](https://www.ruby-forum.com/u/GeorgeTWalters)\
**Replies:** 2\
**Last updated:** [August 18, 2019, 5:44am UTC](https://www.ruby-forum.com/t/can-rspec-take-in-a-variable-from-the-command-line-and-use-that-variable-in-all-tests/252887 "2019-08-18T05:44:32Z")

</div>

I have a variable that comes in from the command line. . . rspec spec spec/boxing/kata/boxing\_kata\_spec.rb \<spec/fixtures/family\_preferences.csv The variable. . . family\_file = $stdin. . .can be seen by the first test …

---

## [How to parse json and access a value in Ruby](https://www.ruby-forum.com/t/how-to-parse-json-and-access-a-value-in-ruby/252104)

<div class="topic-metadata">

**Author:** [@ameha](https://www.ruby-forum.com/u/ameha)\
**Replies:** 1\
**Last updated:** [March 18, 2019, 10:52am UTC](https://www.ruby-forum.com/t/how-to-parse-json-and-access-a-value-in-ruby/252104 "2019-03-18T10:52:10Z")

</div>

How can I get the value of id, description and count. I did JSON.parse(response) but I am not sure how to get the value. { “students”: \[ { “name”: “test1”, “id”: 1, “created\_at”: “2019-03-13T21:34:30Z”, “updated\_a…

---

## [Scripts fails to identify objects using Ruby version 2.2.6 and RubyMine version 2018 on VM with windows 7](https://www.ruby-forum.com/t/scripts-fails-to-identify-objects-using-ruby-version-2-2-6-and-rubymine-version-2018-on-vm-with-windows-7/251757)

<div class="topic-metadata">

**Author:** [@Priya.asfriend](https://www.ruby-forum.com/u/Priya.asfriend)\
**Replies:** 0\
**Last updated:** [January 29, 2019, 8:28am UTC](https://www.ruby-forum.com/t/scripts-fails-to-identify-objects-using-ruby-version-2-2-6-and-rubymine-version-2018-on-vm-with-windows-7/251757 "2019-01-29T08:28:08Z")

</div>

Scripts fails to identify objects using Ruby version 2.2.6 and RubyMine version 2018 on VM with windows 7. But same scripts works fine on Windows 10 Scripts are failing to identify object and thus fails on very first st…

---

## [Mocking or stubbing an external database](https://www.ruby-forum.com/t/mocking-or-stubbing-an-external-database/237320)

<div class="topic-metadata">

**Author:** [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Replies:** 1\
**Last updated:** [October 18, 2018, 6:07pm UTC](https://www.ruby-forum.com/t/mocking-or-stubbing-an-external-database/237320 "2018-10-18T18:07:24Z")

</div>

I’m working on a system that requires me to attach to an external (readonly) database. In order to prevent Rails establishing a connection to the external database for every test, I’ve written something like this: m…

---

## [\[Rspec\] How do you nest before(:all) or after(:all) blocks?](https://www.ruby-forum.com/t/rspec-how-do-you-nest-before-all-or-after-all-blocks/159407)

<div class="topic-metadata">

**Author:** [@johnk](https://www.ruby-forum.com/u/johnk)\
**Replies:** 8\
**Last updated:** [October 24, 2013, 8:39pm UTC](https://www.ruby-forum.com/t/rspec-how-do-you-nest-before-all-or-after-all-blocks/159407 "2013-10-24T20:39:56Z")

</div>

Hi all, I’m new to Rspec but loving it so far and looking to use it as a replacement for a Test::Unit framework I have which drives a web app via Watir. So far, things have worked very well with Rspec but I can’t get…

---

## [Template Scaffold syntax error](https://www.ruby-forum.com/t/template-scaffold-syntax-error/236457)

<div class="topic-metadata">

**Author:** [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Replies:** 1\
**Last updated:** [October 23, 2013, 12:19pm UTC](https://www.ruby-forum.com/t/template-scaffold-syntax-error/236457 "2013-10-23T12:19:22Z")

</div>

I inherited a project from another developer. I ran rspec . and I and getting the following error and I need help figuring it out asap. ~/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/configuration.r…

---

## [RSpec with devise test helpers](https://www.ruby-forum.com/t/rspec-with-devise-test-helpers/236982)

<div class="topic-metadata">

**Author:** [@alcina](https://www.ruby-forum.com/u/alcina)\
**Replies:** 0\
**Last updated:** [October 16, 2013, 9:17pm UTC](https://www.ruby-forum.com/t/rspec-with-devise-test-helpers/236982 "2013-10-16T21:17:24Z")

</div>

I’ve set my Rails (engine) project to include devise and other tools like FactoryGirl. I’d like to write some tests with contexts around anon/authenticated users. In my spec\_helper.rb’s RSpec.configure block, I have …

---

## [How to write “Destroy kid” in cucumber](https://www.ruby-forum.com/t/how-to-write-destroy-kid-in-cucumber/236711)

<div class="topic-metadata">

**Author:** [@alcina](https://www.ruby-forum.com/u/alcina)\
**Replies:** 0\
**Last updated:** [September 27, 2013, 9:37am UTC](https://www.ruby-forum.com/t/how-to-write-destroy-kid-in-cucumber/236711 "2013-09-27T09:37:25Z")

</div>

how to write “Destroy kid” in cucumber Scenario: User can delete kids Given I am on the kids page When I Destroy kid Then I should see “Kid deleted successfully” Then one kid should not exist Listing kids Kid nam…

---

## [Take screenshot even if the test case get passed](https://www.ruby-forum.com/t/take-screenshot-even-if-the-test-case-get-passed/236486)

<div class="topic-metadata">

**Author:** [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Replies:** 1\
**Last updated:** [September 6, 2013, 1:43pm UTC](https://www.ruby-forum.com/t/take-screenshot-even-if-the-test-case-get-passed/236486 "2013-09-06T13:43:51Z")

</div>

Hi, I am using selenium webdriver and rspec so when a test case fail a screenshot is taken. But is there any way to take a screenshot for all test cases so even when the test case pass? Thanks in advance! Mattias

---

## [How to catch the mocked infinite loop behavior and handle it with a rescue block using RSpec](https://www.ruby-forum.com/t/how-to-catch-the-mocked-infinite-loop-behavior-and-handle-it-with-a-rescue-block-using-rspec/235868)

<div class="topic-metadata">

**Author:** [@alcina](https://www.ruby-forum.com/u/alcina)\
**Replies:** 0\
**Last updated:** [July 20, 2013, 7:19pm UTC](https://www.ruby-forum.com/t/how-to-catch-the-mocked-infinite-loop-behavior-and-handle-it-with-a-rescue-block-using-rspec/235868 "2013-07-20T19:19:37Z")

</div>

I have a Book Model which is a ruby script that assigns prices to certain predefined Book titles mentioned in the program. I’m using Ruby 1.9.3-p327 and rspec 2.11.0 #class RspecLoopStop \< Exception; end class Book …

---

## [RSpec2 error codes testing](https://www.ruby-forum.com/t/rspec2-error-codes-testing/235863)

<div class="topic-metadata">

**Author:** [@alcina](https://www.ruby-forum.com/u/alcina)\
**Replies:** 0\
**Last updated:** [July 20, 2013, 2:20pm UTC](https://www.ruby-forum.com/t/rspec2-error-codes-testing/235863 "2013-07-20T14:20:02Z")

</div>

I have an issue with testing error codes. If a record is not found, it raises exception ActiveRecord::RecordNotFound. The thing is, I wanna check if 404 is returned. If I go ahead and just check : response.status.shou…

---

## [RSpec can't define singleton error when trying to mock gets and puts for Hash Elements](https://www.ruby-forum.com/t/rspec-cant-define-singleton-error-when-trying-to-mock-gets-and-puts-for-hash-elements/245878)

<div class="topic-metadata">

**Author:** [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Replies:** 0\
**Last updated:** [July 20, 2013, 11:50am UTC](https://www.ruby-forum.com/t/rspec-cant-define-singleton-error-when-trying-to-mock-gets-and-puts-for-hash-elements/245878 "2013-07-20T11:50:21Z")

</div>

I have a Book Model which is a ruby script that assigns prices to certain predefined Book titles mentioned in the program. Here’s how the book model looks:- class Book attr\_accessor :books def initialize books …

---

## [I18n\_sample\_app](https://www.ruby-forum.com/t/i18n-sample-app/235637)

<div class="topic-metadata">

**Author:** [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Replies:** 0\
**Last updated:** [July 4, 2013, 10:22pm UTC](https://www.ruby-forum.com/t/i18n-sample-app/235637 "2013-07-04T22:22:23Z")

</div>

Hello, Im new to ruby and started with the railstutorial.org from Michael Hartl, after ending it im trying to translate it to other languages but i got stuck how to i18n rspec tests, could you please provide some new…

---

## [Trouble with Capybara and RSpec on a new model spec](https://www.ruby-forum.com/t/trouble-with-capybara-and-rspec-on-a-new-model-spec/228659)

<div class="topic-metadata">

**Author:** [@code-vortex](https://www.ruby-forum.com/u/code-vortex)\
**Replies:** 1\
**Last updated:** [June 18, 2013, 1:55pm UTC](https://www.ruby-forum.com/t/trouble-with-capybara-and-rspec-on-a-new-model-spec/228659 "2013-06-18T13:55:49Z")

</div>

I have searched a lot for a solution, but I could not find it! I am trying to make a rails application after watching Michael H.'s tutorial and when i fill data manually and submit it nothing happens and nothing recor…

---

## [How to generate TAP output for Selenium-WebDriver TC written in Ruby](https://www.ruby-forum.com/t/how-to-generate-tap-output-for-selenium-webdriver-tc-written-in-ruby/235246)

<div class="topic-metadata">

**Author:** [@alcina](https://www.ruby-forum.com/u/alcina)\
**Replies:** 0\
**Last updated:** [June 9, 2013, 7:29am UTC](https://www.ruby-forum.com/t/how-to-generate-tap-output-for-selenium-webdriver-tc-written-in-ruby/235246 "2013-06-09T07:29:32Z")

</div>

Hello, We need to integrate Jenkins with TestLink. For now Iam able to run Selenium-WebDriver test cases from Jenkins using the inputs from TestLink. Now the problem is the reverse communication where the Jenkins tes…

---

## [RSpec 2, Rails 3, and SSL routes](https://www.ruby-forum.com/t/rspec-2-rails-3-and-ssl-routes/200362)

<div class="topic-metadata">

**Author:** [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Replies:** 5\
**Last updated:** [June 4, 2013, 1:01pm UTC](https://www.ruby-forum.com/t/rspec-2-rails-3-and-ssl-routes/200362 "2013-06-04T13:01:48Z")

</div>

Hi all, This is probably a stupid question, but I can’t figure out how to do this… If I have some routes which require SSL, how do I test that with RSpec? For example: routes.rb scope :constraints =\> { :protocol =\> …

---

## [Warning: toplevel constant XYZ referenced Admin:XYZ](https://www.ruby-forum.com/t/warning-toplevel-constant-xyz-referenced-admin-xyz/205807)

<div class="topic-metadata">

**Author:** [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Replies:** 7\
**Last updated:** [May 15, 2013, 4:40pm UTC](https://www.ruby-forum.com/t/warning-toplevel-constant-xyz-referenced-admin-xyz/205807 "2013-05-15T16:40:50Z")

</div>

I have an odd problem. I got controllers in a namespace and controllers outside of the namespace. For example, I have a PagesController and a Admin::PagesController. When I run rspec from the top, tests pass and I get…

---

## [STDOUT.should\_receive(:puts).with Works \*\*\* (:print) fails](https://www.ruby-forum.com/t/stdout-should-receive-puts-with-works-print-fails/228882)

<div class="topic-metadata">

**Author:** [@godfather667](https://www.ruby-forum.com/u/godfather667)\
**Replies:** 0\
**Last updated:** [May 11, 2013, 5:36am UTC](https://www.ruby-forum.com/t/stdout-should-receive-puts-with-works-print-fails/228882 "2013-05-11T05:36:43Z")

</div>

I use: STDOUT.should\_receive(:puts).with… many times in RSPEC However, I have a couple of “do you want(Y/N)?” where I use ‘print’ and apparently …(:print).with… does not work. I can change ‘print’ to ‘puts’ and it …

---

## [Regular expression hang ruby.exe](https://www.ruby-forum.com/t/regular-expression-hang-ruby-exe/228798)

<div class="topic-metadata">

**Author:** [@maetthew](https://www.ruby-forum.com/u/maetthew)\
**Replies:** 1\
**Last updated:** [May 8, 2013, 9:42am UTC](https://www.ruby-forum.com/t/regular-expression-hang-ruby-exe/228798 "2013-05-08T09:42:42Z")

</div>

Hi, I use an regular expression to verify that my string in time\_on\_site is correct. This works when the string match, but if it doesn’t the test get hanged. So I need to quit(ctrl+c) the script and also close ruby.ex…

---

## [Cookie inspection in Rspec and Capybara](https://www.ruby-forum.com/t/cookie-inspection-in-rspec-and-capybara/228411)

<div class="topic-metadata">

**Author:** [@assafshomer](https://www.ruby-forum.com/u/assafshomer)\
**Replies:** 0\
**Last updated:** [April 21, 2013, 9:25am UTC](https://www.ruby-forum.com/t/cookie-inspection-in-rspec-and-capybara/228411 "2013-04-21T09:25:49Z")

</div>

Hi. I’m trying to inspect an authentication cookie inside of a test. My goal is to test a remember me functionality, so I would like to test for the expiration date of the set cookie. This turns out to be extremely d…

---

## [Rspec + include Rails.application.routes.url\_helpers](https://www.ruby-forum.com/t/rspec-include-rails-application-routes-url-helpers/227754)

<div class="topic-metadata">

**Author:** [@edelpero](https://www.ruby-forum.com/u/edelpero)\
**Replies:** 0\
**Last updated:** [March 27, 2013, 9:06pm UTC](https://www.ruby-forum.com/t/rspec-include-rails-application-routes-url-helpers/227754 "2013-03-27T21:06:41Z")

</div>

Hello there, I’m having some issues trying to test a custom helper. And I been trying to figure this out without luck. Please, see attachs for more information. Thanks in advance.

---

## [Rspec + Devise + BaseController](https://www.ruby-forum.com/t/rspec-devise-basecontroller/227594)

<div class="topic-metadata">

**Author:** [@edelpero](https://www.ruby-forum.com/u/edelpero)\
**Replies:** 1\
**Last updated:** [March 20, 2013, 7:21pm UTC](https://www.ruby-forum.com/t/rspec-devise-basecontroller/227594 "2013-03-20T19:21:12Z")

</div>

Hello there, I’m creating a base controller for the admin section of a project. All controllers whitin the admin section will inherit from it. ##################################################### #app/controllers/ad…

[Next page](https://www.ruby-forum.com/c/testing/rspec/25.md?page=1)
