rspec

How to properly test ActiveJob's retry_on method with rspec?

此生再无相见时 提交于 2020-05-14 14:57:29
问题 I have been attempting to test this method for the past few days with no luck. Another thing I'd like to be able to do is rescue the error that bubbles up after the final retry attempt is made. Please see my comments and code snippets below. Source code for retry_on is here as well for context. Here's the sample code and tests: my_job.rb retry_on Exception, wait: 2.hours, attempts: 3 do |job, exception| # some kind of rescue here after job.exceptions == 3 # then notify Bugsnag of failed final

RSpec partial match against a nested hash

心不动则不痛 提交于 2020-05-09 20:34:39
问题 I've got a JSON structure that I'd like to match a single nested element in, while ignoring other data. The JSON looks like this (minimally): { "employee": { "id": 1, "jobs_count": 0 }, "messages": [ "something" ] } Here's what I'm using right now: response_json = JSON.parse(response.body) expect(response_json).to include("employee") expect(response_json["employee"]).to include("jobs_count" => 0) What I'd like to do is something like: expect(response_json).to include("employee" => { "jobs

rspec - How to test ActiveRecord::RecordNotFound?

允我心安 提交于 2020-04-17 22:06:34
问题 I have a method to update people's attribute, and it will rescue ActiveRecord::RecordNotFound if the people cannot be found. The method is: def update @people= People.find(params[:id]) if @people.update(people_params) render json: { success: 'Success' } else render :edit end rescue ActiveRecord::RecordNotFound => e render json: { error: 'Failed') } end And I want to test the situation when the record not found, here's my test for now: let(:people) { create(:people) } let(:people_id) { people

How to expect some (but not all) arguments with RSpec should_receive?

假装没事ソ 提交于 2020-04-07 02:34:40
问题 class Foo def bar(a, b) ... Foo.should_receive( :bar ) expects bar to be called with any arguments. Foo.should_receive( :bar ).with( :baz, :qux ) expects :baz and :qux to be passed in as the params. How to expect the first param to equal :baz, and not care about the other params? 回答1: Use the anything matcher: Foo.should_receive(:bar).with(:baz, anything) 回答2: For Rspec 1.3 anything doesn't work when your method is receiving a hash as an argument, so please try with hash_including(:key => val

How to expect some (but not all) arguments with RSpec should_receive?

南楼画角 提交于 2020-04-07 02:34:35
问题 class Foo def bar(a, b) ... Foo.should_receive( :bar ) expects bar to be called with any arguments. Foo.should_receive( :bar ).with( :baz, :qux ) expects :baz and :qux to be passed in as the params. How to expect the first param to equal :baz, and not care about the other params? 回答1: Use the anything matcher: Foo.should_receive(:bar).with(:baz, anything) 回答2: For Rspec 1.3 anything doesn't work when your method is receiving a hash as an argument, so please try with hash_including(:key => val

How to test callback that uses parent record in Rspec Feature spec

早过忘川 提交于 2020-03-25 18:21:01
问题 I have a feature spec to create an order . That order has a callback like so: class Order belongs_to :site before_validation :add_call_number def call_number self.call_number = self.site.call_number_start end end In the spec, I get a NoMethod error on call_number_start , because self.site doesn't exist. I discovered that the Site I created in the Rspec before action doesn't exist at all. In other words... require 'rails_helper' describe "create successfully", type: :feature, js: true do

Invalid access_token when using RSpec request specs to authorize a request

梦想的初衷 提交于 2020-03-23 15:31:28
问题 I'm trying to test CredentialsController , which works fine in production, using RSpec request specs. Code Controller class CredentialsController < ApplicationController before_action :doorkeeper_authorize! def me render json: current_user end end ( GET /me routes to CredentialsController#me .) Request Specs describe 'Credentials', type: :request do context 'unauthorized' do it "should 401" do get '/me' expect(response).to have_http_status(:unauthorized) end end context 'authorized' do let!(

Invalid access_token when using RSpec request specs to authorize a request

寵の児 提交于 2020-03-23 15:30:14
问题 I'm trying to test CredentialsController , which works fine in production, using RSpec request specs. Code Controller class CredentialsController < ApplicationController before_action :doorkeeper_authorize! def me render json: current_user end end ( GET /me routes to CredentialsController#me .) Request Specs describe 'Credentials', type: :request do context 'unauthorized' do it "should 401" do get '/me' expect(response).to have_http_status(:unauthorized) end end context 'authorized' do let!(

RSpec: How to Stub Only One of Multiple Calls to Same Method

故事扮演 提交于 2020-03-23 12:40:31
问题 I'm having trouble figuring out how to stub only one of two calls to a method. Here's an example: class Example def self.foo { a: YAML.load_file('a.txt'), # don't stub - let it load b: YAML.load_file('b.txt') } # stub this one end end RSpec.describe Example do describe '.foo' do before do allow(YAML).to receive(:load_file).with('b.txt').and_return('b_data') end it 'returns correct hash' do expect(described_class.foo).to eq(a: 'a_data', b: 'b_data') end end end The test fails because I have

Shoulda belongs_to with class_name and foreign_key

限于喜欢 提交于 2020-03-21 10:57:33
问题 I know you can easily test a belongs to relationship using Shoulda: describe Dog dog it { should belong_to(:owner) } end Is it possible to test a more complicated belongs_to relationship using Shoulda? Something like this: class Dog < ActiveRecord::Base belongs_to :owner, :class_name => "Person", :foreign_key => "person_id" end 回答1: You should be able to use: it { should belong_to(:owner).class_name('Person') } Shoulda's belong_to matcher always reads the foreign_key from the association and