How to ignore or skip a test method using RSpec?
You can use pending()
or change it
to xit
or wrap assert in pending block for wait implementation:
describe 'Automation System' do
# some code here
it 'Test01' do
pending("is implemented but waiting")
end
it 'Test02' do
# or without message
pending
end
pending do
"string".reverse.should == "gnirts"
end
xit 'Test03' do
true.should be(true)
end
end
Programmatically skip a test in RSpec
I asked in the rspec mailing list, and it turns out skip 'reason'
does the trick. The example is marked as pending, which is not ideal, but that answers the question.
In Rails, can you ignore specific tests from running?
In RSpec one can use exclusion filters and then from the command line, skip specific tests.
In your case, tag the description blocks as java: true
.
describe "code for JVM in production", java: true do
it "java-specific test" do
end
end
Then run rspec . --tag ~java:true
RSpec will ignore/skip the tests matching java: true
tag.
NOTE: It is not necessary to set the other tests to java: false
Alternatively, you can amend your spec_helper.rb with a configuration to skip these tests when run locally using an environment variable.
RSpec.configure do |c|
if RUBY_PLATFORM.include?('darwin') # assumes Macintosh
c.filter_run_excluding java: true
end
end
CITE:
- https://relishapp.com/rspec/rspec-core/v/2-5/docs/filtering/exclusion-filters
- https://www.relishapp.com/rspec/rspec-core/v/2-4/docs/command-line/tag-option
- Detecting Operating Systems in Ruby
How to skip part of method code in Rails specs
Ok I managed it by using receive_message_chain
helper. Specs should looked like this:
describe 'finalize inquiry process' do
subject(:process_update) do
described_class.new(
inquiry_process: inquiry_process,
form: loan_application_inquiry_process_update_form,
finalize_process: true,
).call
end
let!(:inquiry_process) do
create :inquiry_process, inquiry_template: loan_inquiry_template, campaign_code_uid: campaign_code.uid
end
before do
allow(InquiryProcessDocumentCreatorFetcher).to receive_message_chain(:new, :call, :new, :call)
end
it 'updates state of assigned campain code' do
updated_inquiry_process = process_update.value!
expect(updated_inquiry_process.campaign_code.state).to eq('used')
end
end
Skip an RSpec test case at runtime
Use exclusion filters.
describe "market a", :market => 'a' do
...
end
describe "market b", :market => 'b' do
...
end
describe "market c", :market => 'c' do
...
end
RSpec.configure do |c|
# Set these up programmatically;
# I'm not sure how you're defining which market is 'active'
c.filter_run_excluding :market => 'a'
c.filter_run_excluding :market => 'b'
# Now only tests with ":market => 'c'" will run.
end
Or better still, use implicit filters.
describe "market a", :if => CurrentMarket.a? do # or whatever
...
end
RSpec in Rails: How to skip a before_filter?
To skip the before filter:
controller.class.skip_before_filter :name_of_method_used_as_before_filter
The one caveat (mentioned the docs) is that this will only work for method-reference filters, not procs.
Alternatively, you could stub current_user.has_role?
describe SomeModelsController, "GET to index (functional)" do
before(:each) do
controller.current_user.stub!(:has_role?).and_return(true)
end
it "should find all Models" do
Model.should_receive(:find).with(:all)
end
end
Rspec prevent method from being called
So you want just to test redirects and the errors occured when calculate_price method executes bother you. Why don't you just stub that method? Your spec file might be like this:
require 'rails_helper'
describe Front::OrdersController, type: :controller do
describe '#new' do
# Set up dummy menu
let (:menu) { Menu.create() }
# Check this out
before do
allow_any_instance_of(Menu).to receive(:calculate_price)
# or if you need certain value
allow_any_instance_of(Menu).to receive(:calculate_price).and_return(your_value)
end
it "redirects user to sign up page if user is present in the system" do
user = User.create(name: "Bob", password: "bobspassword", phone: "+7 (903) 227-8874")
get :new, params: { phone: user.phone }
expect(response).to redirect_to(new_user_session_path(phone: user.phone))
end
it "redirects user to new order page if user is not present in the system" do
non_present_phone = "+7 (903) 227-8874"
get :new, params: { phone: non_present_phone, menu_id: menu.id}
expect(response).to redirect_to(new_order_path)
end
end
end
Related Topics
Good Explanation of Ruby Object Model -- Mainly, 'Classes Are Objects'
How to Integrate Hoptoad with Delayedjob and Daemonspawn
Setting Up Env, Osx Rbenv and Bundle Battle
Sequel Accessing Many_To_Many Join Table When Adding Association
What Is the Most Elegant Way in Ruby to Remove a Parameter from a Url
Override the Protect_From_Forgery Strategy in a Controller
How to Do Case-Insensitive Order in Rails with Postgresql
How to Get a Remote-File's Mtime Before Downloading It in Ruby
Is There a Function Like String#Scan, But Returning Array of Matchdatas
Convert a Partial to Method/Block for Speed
How to Find a Memory Leak on Heroku
Elegantly Selecting Attributes from Has_Many :Through Join Models in Rails
Changing Value of Ruby Variables/References