rspec

How to check if rake task was finished?

岁酱吖の 提交于 2021-01-28 09:24:11
问题 This is my first experience with RoR and I using the rspec tool to test my controller. But inside the methods, I using rake tasks to create asynchronous solutions like this: def scan_all_urls system 'rake scan_all_urls &' end And my rake task is this: task :scan_all_urls => :environment do crawler = Crawler.new urls = Url.all crawler.scan_urls(urls) end But my issue is, at the rspec test: it 'should scan all url' do params = {} get 'scan_all_urls', params: params, format: :json end How I

RSpec controller for nested resources, before_action

十年热恋 提交于 2021-01-28 07:10:26
问题 Let say I have setup like this: config/routes.rb resources :graves do resources :candles end app/models/grave.rb has_many :candles, dependent: :destroy app/models/candles.rb belongs_to :grave app/controllers/candles_controller.rb before_action :get_grave def create @candle = @grave.candles.new(candle_params) respond_to do |format| if @candle.save format.html { redirect_to @grave, notice: 'Candle was successfully created.' } format.json { } else format.html { render :new } format.json { render

Unable to use OptionParser and rspec

和自甴很熟 提交于 2021-01-28 05:08:13
问题 I have a simple watir (web-driver) script which goes to google. But, I want to use option parser to set an argument in the cmd to select a browser. Below is my script: require 'optparse' require 'commandline/optionparser' include CommandLine require 'watir-webdriver' describe 'Test google website' do before :all do options = {} opts = OptionParser.new do |opts| opts.on("--browser N", "Browser to execute test scripts") do |n| options[:browser] = n $b = n.to_s end end opts.parse! ARGV p options

Rails Rspec controller testing with nested resources

岁酱吖の 提交于 2021-01-28 04:48:44
问题 I have nested resources: resources :requests do resources :responses end and want to write controller test for response model. When I try to write: RSpec.describe ResponsesController, type: :controller do describe 'GET #show' do before do @testrequest = Request.create @testresponse = @testrequest.responses.create get :show, request_id: @testrequest.id, id: @testresponse.id end it 'show specific response selected by id if user owner of response or user owner of parent request' do expect

How to check block is called using rspec

那年仲夏 提交于 2021-01-28 00:29:09
问题 I want to check whether the block is called in my function using rspec. Below is my code: class SP def speak(options={},&block) puts "speak called" block.call() rescue ZeroDivisionError => e end end describe SP do it "testing speak functionality can receive a block" do sp = SP.new def test_func a = 1 end sp_mock = double(sp) expect(sp_mock).to receive(:speak).with(test_func) sp.speak(test_func) end end Below is my error: SP testing speak functionality can receive a block Failure/Error: block

How to delete quotation marks in an array in Ruby

让人想犯罪 __ 提交于 2021-01-27 13:40:11
问题 I am working on Test First Ruby with rspec examples testing... which I need to pass this test. it "tokenizes a string" do calculator.tokens("1 2 3 * + 4 5 - /").should == [1, 2, 3, :*, :+, 4, 5, :-, :/] end And here is my code def tokens(str) data = str.split(' ') outcomes = [] data.collect do |x| if x.to_i != 0 outcomes.push(x.to_i) elsif x.to_i == 0 temp = x.gsub('"', '') outcomes.push(":#{temp}") end end outcomes end However, I got these feedback. Have no idea how to get rid of the

Pretty-print arrays on failure

℡╲_俬逩灬. 提交于 2021-01-27 08:21:29
问题 describe Rspec do it 'should print arrays in a readable manner' do arr = [ [0, :a, -1], [1, :b, -2], [2, :c, -3], [3, :d, -4], [4, :e, -5], [6, :g, -7], [7, :h, -8], [8, :i, -9] ] arr.should eql [] end end On failure: Failures: 1) Rspec should print arrays in a readable manner Failure/Error: arr.should eql [] expected: [] got: [[0, :a, -1], [1, :b, -2], [2, :c, -3], [3, :d, -4], [4, :e, -5], [6, :g, -7], [7, :h, -8], [8, :i, -9]] Is there a way to tell Rspec to pretty print its failures? My

Pretty-print arrays on failure

孤街醉人 提交于 2021-01-27 08:21:22
问题 describe Rspec do it 'should print arrays in a readable manner' do arr = [ [0, :a, -1], [1, :b, -2], [2, :c, -3], [3, :d, -4], [4, :e, -5], [6, :g, -7], [7, :h, -8], [8, :i, -9] ] arr.should eql [] end end On failure: Failures: 1) Rspec should print arrays in a readable manner Failure/Error: arr.should eql [] expected: [] got: [[0, :a, -1], [1, :b, -2], [2, :c, -3], [3, :d, -4], [4, :e, -5], [6, :g, -7], [7, :h, -8], [8, :i, -9]] Is there a way to tell Rspec to pretty print its failures? My

Make capybara wait until Ajax finishes

只愿长相守 提交于 2021-01-07 08:23:09
问题 I have a feature test, where a list of todos are listed on a page and you click on first todo to see the details of that todo. The first todo link has ID todo_1. The issue I'm facing is that I don't know how to wait for the link to appear before I visit it. The todos are listed in a table using an AJAX call. 回答1: Capybara has waiting behavior built in to most of its finders and actions, so you normally don't have to wait specifically for the ajax to finish since Capybara waits for the visible

Make capybara wait until Ajax finishes

随声附和 提交于 2021-01-07 08:22:27
问题 I have a feature test, where a list of todos are listed on a page and you click on first todo to see the details of that todo. The first todo link has ID todo_1. The issue I'm facing is that I don't know how to wait for the link to appear before I visit it. The todos are listed in a table using an AJAX call. 回答1: Capybara has waiting behavior built in to most of its finders and actions, so you normally don't have to wait specifically for the ajax to finish since Capybara waits for the visible