Any way to test that a method starts pry? - ruby

I'm building a gem that allows for a much more convenient and configurable 'console' for ruby gem development than the current options (ie: 'bundle console').
As such, one of if not the most important aspects of the entire gem is that it does in fact open a console session, which I currently have set up in a start method:
class MyConsole
def start
Pry.start(self)
end
end
I am trying to test this functionality, but it's difficult because their's not a lot of good resources for this. It's also really annoying because every time I run this method in rspec, pry opens, and I have to exit it before finishing the rest of the tests.
I have three main questions:
How can I run a method in rspec that starts pry without actually starting pry?
How can I specifically test that that method does in fact start pry?
Assuming 1 and 2 are possible, how can I test what context the method starts pry in? (notice how the start method calls Pry.start(self), meaning the pry session should open in the context of a MyConsole instance.)

The best way to do this would probably be using an RSpec spy.
Your test would probably look something like this:
describe MyConsole do
describe '#start' do
it 'calls Pry.start' do
described_class.start
expect(Pry).to have_received(:start).with('your_args_here')
end
end
end
This is a really good explanation of RSpec stubbing options, IMO, where you can learn more: https://about.futurelearn.com/blog/stubs-mocks-spies-rspec

Related

RSpec stubs apparently not cleaned up after test

I've been working to diagnose a test failure that only occurs on my master branch. Following is the relevant code, in simplified form.
There's a service:
class Service
attr_reader :traces
def initialize
#traces = []
end
def do_work
#traces << Thread.current.backtrace
# ... actual work ...
end
end
And a class that makes use of the service:
class Widget
def get_cached_service
puts("Getting a cached service!")
puts("Do I already have one? #{!!#service}")
#service ||= make_service
end
def make_service
puts("Making a service!")
Service.new
end
end
I have a test (that lives in a file widget_spec.rb) that fails intermittently. This test creates an instance of Widget and calls get_cached_service. I see the Getting a cached service! message on the console, followed by Do I already have one? false, but I don't see the Making a service! message.
Furthermore, when I examine the traces attribute of the returned Service object, I find stack traces originating from other tests in my project (eg. foo_spec.rb, bar_spec.rb, etc).
In a few different places I find code like:
allow_any_instance_of(Widget)
.to receive(:make_service).and_return(whatever)
The other tests whose stack traces I find are likely stubbing make_service like this. But it appears that the stubbing is not being undone after those tests, as should always happen according to my understanding.
Is there any reason, other than a bug in rspec, that could cause a stub not to be reset at the end of a test?
The stub is almost certainly being cleared, but you’ve cached the fake instance in get_cached_service. Nothing clears the cached value in #service, and RSpec (rightfully) doesn’t know about it. As such, stubbing make_service is not enough if tests call get_cached_service. You have a few options:
Always stub get_cached_service instead of, or in addition to, make_service
Provide a way to clear the cached value which is called after each test.
Make the caching configurable in some way, or a wrapper around the actual implementation, such that the caching does not occur in test code.
I realise this is quite late to answer, but for posterity for anyone who reads this:
Use rspec bisect to figure out if there is a consistent test ordering that causes failure, then start ripping code out until you're left with only the bit that breaks.
I can't remember a case where RSpec is at fault - almost invariably, somewhere there is a class variable that isn't getting cleared, or someone is manually playing with a class with something like define_method. Occasionally it might be happening in a gem.
Make sure everything is cleared after every test in your spec_helper - clear the Rails cache, clear ActionMailer deliveries, return from Timecop freezes, etc.
Anything directly RSpec-related should clear itself in theory, because it's designed to integrate into RSpec, and is probably the least likely explanation in general.

How can I workaround this capybara/rspec naming collision?

I'm writing a new rspec test case using Capybara (SitePrism actually, which uses Capybara) and I've run into an apparently known issue: https://github.com/jnicklas/capybara/issues/1396. Essentially, due to a change in one or the other, RSpec and Capybara now both have methods named all, and when I try to use SitePrism to find a group of elements or SitePrism sections, Capybara invokes the wrong method and returns something of the type RSpec::Matchers::BuiltIn::All rather than the expected array of Capybara or SitePrism objects.
For some reason, all my old tests, including many with very similar usage of sections and elements constructs, work perfectly fine. I'm having a really hard time finding differences between them that would account for one failing and the other succeeding. I briefly tried rolling back either Capybara or RSpec to just try to make the problem go away for the moment, but it seemed silly, trying to pinpoint when the problem was introduced, when existing test cases that have been running every day never broke.
Can anyone advise me on why one works and the other fails? Here is what these two test cases have in common:
Both spec files require the same spec_helper.rb file.
The spec_helper.rb file has both require rspec and require capybara.
Each spec file uses require_relative to require a page object used in each test.
Each page object file has a sections :table_rows, <SECTION CLASS>, <ROW CSS> declaration. They may have different names, classes, and CSS Selectors, but they're the same basic construct.
In each spec file, methods are invoked on the page objects that reference table_rows.
Referencing table_rows in one of these older test cases works just fine, but I'm getting the name collision error in the new test case. Anyone know why that might be, so I can fix the new test case?
Failing that, does anyone know how I can separate things so that the page object requires capybara but not rspec and the spec file requires rspec but not capybara to prevent the collision? I don't know much about Ruby package management, but it seems like in order to run an rspec test case that uses a page object, it's all going to be mixed together. I'm not sure of a way to avoid requiring them both.
Failing that, does anyone know which versions of either of these two packages I could theoretically use to avoid the issue? I did experiments with rspec 3.2 and capybara 2.4, and neither seemed to work, and I gave up there, because I remembered that no matter how far back in time I go, the test cases I've already written were working fine, and it just seemed silly to try to solve it this way.
Require capybara after Rspec or always call page.all rather than just all
Not the best approach, but you can monkey patch the SitePrism::Page class. Here you replace 'all' by 'page.all':
SitePrism::Page.class_eval do
def find_all(*find_args)
page.all(*find_args)
end
end

RSPEC: Is it possible to run other spec file inside a spec file

I know best practice is to make tests run independently.
I am using Rspec to run Selenium Webdriver tests. If I create and teardown users-groups-other models for every specific test case, the test run time goes up significantly (30 mins or so).
So I separated the spec files into create_..., delete_..., and would like to call these spec files inside one spec file. Is this possible and is it a good or bad way of setting up the tests?
Something like:
create_users_spec.rb
create_user_groups_spec.rb
create_dashboard_spec.rb
==> run some tests....
delete_dashboard_spec.rb
delete_user_groups_spec.rb
delete_users_spec.rb
run_all_spec.rb => run all of the spec files above, so you can see that
it needs to run in a certain sequence
You really want to be able to run tests independently. Having to run all tests, even "just" all tests in a given file, seems to me like an unacceptable delay in the development process. It also makes in hard to use tools like Guard that run tests for you based on a file watcher.
If there is really something that needs to run before, for example, every request spec, you can do something like:
# spec_helper.rb
RSpec.configure do |config|
config.before :all, type: :request do
# do stuff
end
end
But also look at what you are trying to do. If you are just setting up model/database data for the tests, then you want fixtures, or better yet, factories. Look into FactoryGirl.
Perhaps organizing your tests with context and using before(:each) and before(:all) judiciously would yield a performance boost. Simplifying the database, or using a library like FactoryGirl can also help.

Rspec Class any_instance expects method_name - where it is documented?

I've stumbled upon the following piece of code in an Rspec test and I must say I more or less figured out what it does but I can't find relevant sources to prove it. Please point me to a gem or docs that describe:
describe SomeModule::Salesforce::Lead do
before do
SomeModule::Salesforce::Lead.any_instance.expects(:materialize)
end
...
end
It seems that for :each example in this spec it sets expectation on any instance of the class described above to receive a call to :materialize method AND it actually redefines the method to do nothing . The last part seems crucial because it avoids connecting to SalesForce in test environment but I can't find confirmation for this.
any_instance is documented under Working with Legacy code
You are correct in that it both sets an expectation and stubs out the original method on any given instance of a class.
Previous versions of RSpec accomplish this by monkeypatch the ruby core classes (Object and BaseObject)
RSpec 3 has a new syntax which does not rely on monkeypatching:
before do
expect_any_instance_of(SomeModule::Salesforce).to receive(:materialize)
end
Ok I've just found that I was looking in wrong sources, it doesn't come from RSpec but from Mocha Mock (expects and any_instance) http://gofreerange.com/mocha/docs/Mocha/Mock.html#expects-instance_method
Thanks #tomasz-pajor #https://stackoverflow.com/users/2928259/tomasz-pajor

Calling a Method - Watir-Webdriver

I am new to automation and I have started using Watir Webdriver to automate a website. However, there are certain pieces of code which can be reused for multiple test cases. How can I group the reusable pieces of code into a method which I can call in every test case ? Could you please provide references or examples ?
You are interested in methods.
Here is an example:
def automation_snippet
#browser.text_field(:id => 'field_id').set 'foo'
end
Later you will probably be interested in classes, page objects, and modules.
This is basic ruby stuff. You will need to make sure that whatever method you want to use is in scope, too.
So, keywords to start with are methods and scope. If you are using Cucumber, then you can define methods in any step definition file and they will be available in all your other Cucumber tests.

Resources