rspec3

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

How do I test the rescue block of a method with rspec mocks 3.3

心已入冬 提交于 2020-06-28 08:21:28
问题 Help me make this test pass: Here is an example of some rspec code, class User attr_accessor :count def initialize @count = 0 end # sometimes raises def danger puts "IO can be dangerous..." rescue IOError => e @count += 1 end #always raises def danger! raise IOError.new rescue IOError => e @count += 1 end end describe User do describe "#danger!" do it "its rescue block always increases the counter by one" do allow(subject).to receive(:'danger!') expect { subject.danger! }.to change(subject,

Stub Faraday request with Webmock - help needed

北城以北 提交于 2020-06-13 09:25:08
问题 I need help stubbing a request using Faraday gem. I am making this request URL='https://secure.snd.payu.com//pl/standard/user/oauth/authorize'.freeze url_encoded = 'grant_type=client_credentials' \ + "&client_id=#{ENV['client_id'}" \ + "&client_secret=#{ENV['client_secret'}" connection = Faraday.new do |con| con.response :oj, content_type: /\bjson$/ con.adapter Faraday.default_adapter end connection.post(URL, url_encoded) which outputs #<Faraday::Response:0x00000000016ff620 @on_complete

rackup (failed to load command)

纵饮孤独 提交于 2020-01-17 08:01:28
问题 When I type: bundle exec rackup it should give some output like this: $ bundle exec rackup [2017-01-29 21:45:56] INFO WEBrick 1.3.1 [2017-01-29 21:45:56] INFO ruby 2.4.0 (2016-12-24) [x86_64-darwin15] [2017-01-29 21:45:56] INFO WEBrick::HTTPServer#start: pid=48002 port=9292 But instead I get this bundler: failed to load command: rackup (/Users/username/.rbenv/versions/2.4.0/bin/rackup) LoadError: cannot load such file -- rails/all « truncated » Here is my gem file: # frozen_string_literal:

Get the actual value of a boolean attribute

南笙酒味 提交于 2020-01-06 01:30:23
问题 I have the span: <span disabled="disabled">Edit Member</span> When I try to get the value of the disabled attribute: page.in_iframe(:id => 'MembersAreaFrame') do |frame| expect(page.span_element(:xpath => "//span[text()='Edit Member']", :frame => frame).attribute('disabled')).to eq("disabled") end I get: expected: "disabled" got: "true" How do I get the value of specified attribute instead of a boolean value? 回答1: The Page-Object gem's attribute method does not do any formatting of the

How to resolve RSpec's deprecation warning about the new expect syntax?

六眼飞鱼酱① 提交于 2020-01-03 10:18:29
问题 When I run this RSpec example, it passes but I'm getting a deprecation warning. context "should have valid detail for signup" do it "should give error for invalid confirm password" do find("#usernamesignup").set("Any_name") find("#mobile_number").set("1234567890") find("#emailsignup").set("mymail@yopmail.com") find("#passwordsignup").set("12345678") find("#passwordsignup_confirm").set("") find(".signin").click sleep 2 page.should have_content "Confirm Password Does not Match" end end Here is

Rails/RSpec toggle cache for a single test

匆匆过客 提交于 2020-01-01 09:33:59
问题 So in my app I can disable the cache for all tests, which would be ideal, but apparently there are a number of legacy tests that rely on the cache being functional. Is there a way to enable the Rails cache for a single RSpec test? Something like: before(:each) do @cache_setting = Rails.cache.null_cache Rails.cache.null_cache = true end after(:each) do Rails.cache.null_cache = @cache_setting end it 'does not hit the cache' do ... end 回答1: in spec_helper.rb RSpec.configure do |config| config

Travis-CI “The command ”bundle exec rake“ exited with 1.” + mystery 404 error

时间秒杀一切 提交于 2019-12-24 15:01:21
问题 bundle exec rake runs all tests perfectly fine locally . However, Travis CI keeps blowing up with Problem accessing /authentication without giving much more info to go on. Here's one of the failed builds: https://travis-ci.org/Nase00/Horizon/builds/48094102 For the life of me, I cannot figure out what is causing an authentication error when Travis tries to run bundle exec rake . Here's the project repo: https://github.com/Nase00/Horizon 回答1: I'm not sure what version of Neo4j Travis uses (

How to raise an exception in an RSpec test

青春壹個敷衍的年華 提交于 2019-12-23 18:52:23
问题 I'm stuck on a test scenario. I have a controller method: def update @object = Object.find params[:id] # some other stuff @object.save rescue ActiveRecord::StaleObjectError # woo other stuff end The first part I test with: context '#update' let(:object) { double } it 'nothing fails' do # some other stuff expect(Object).to receive(:find).with('5').and_return(object) expect(object).to receive(:save).and_return(true) xhr :put, :update, id:5 expect(response).to be_success expect(assigns(:object))

How to count RSpec examples filtered with :focus in a git hook?

旧街凉风 提交于 2019-12-23 12:10:10
问题 I am trying to write a Git pre-commit hook that would not let the user commit if there is an example that is tagged with :focus . Using RSpec's API (okay even if it is private), is there any way to find out the number of examples with the :focus filter? I found the example_count-instance_method. It could be useful but I'm not sure how it can be called from an external script. 回答1: Here is an Overcommit pre_commit hook that uses RSpecs private API to find out specs with :focus filter: require