[rspec-users] mocking or stubbing an external database

2013-11-10 Thread Fearless Fool
I'm working on a system that requires me to attach to an external (readonly) database. In order to prevent Rails establishing a connection to the external database for every test, I've written something like this: module External class ExternalDBModel < ActiveRecord::Base establish_co

[rspec-users] expecting one of two conditions

2013-03-09 Thread Fearless Fool
I'm expecting my_test to raise one error or another, but since I'm pulling data from a db, I don't know which error it will be. Is there a better way to write this? expect { my_test }.to raise_error { |error| error.should satisfy {|e| e.instance_of?(OneError) || e.instance_of?(OtherError)

Re: [rspec-users] Drawing the line between controller and integration tests

2012-09-10 Thread Fearless Fool
One addendum: One thing I'd specifically like to know is where/how do you test the *contents* of generated responses? That seems beyond the purview of a controller test, but it doesn't require multiple transactions or cross-controller interactions that would require an integration test. -- P

[rspec-users] Drawing the line between controller and integration tests

2012-09-10 Thread Fearless Fool
I'm trying to understand what belongs -- and what doesn't belong -- in controller tests and in integration tests. As a common example, assume I have a list of named widgets. Somewhere in my code, I want to verify that widget = FactoryGirl.create(:widget) get :index generates a page that

[rspec-users] isolating controller tests from models

2012-03-30 Thread Fearless Fool
This is a rehash of a question I posed at: http://stackoverflow.com/questions/9952317/isolating-controller-tests-from-models The basic question: If I have a FoosController and a Foo model, can I run FoosController rspec tests without creating the foos database table? I haven't found a good way t

[rspec-users] mocking a reference to a polymorphic model?

2012-03-22 Thread Fearless Fool
The basic question: how do you mock a reference to a polymorphic class in RSpec? Trying the obvious thing leads to an error of the form: undefined method `base_class' for Post:Class === The details: # file: app/models/relation.rb class Relation < ActiveRecord::Base belongs_to :entity, :pol

Re: [rspec-users] stub().and_raise() ain't raising?

2012-01-12 Thread Fearless Fool
Justin Ko wrote in post #1040632: > What are the failed expectations? Just paste it. You've identified the problem (see below), but to answer your question: 1) Wizard electricity credentials with valid credentials should spawn external loader Failure/Error: @wizard.electricity_loader.shou

[rspec-users] stub().and_raise() ain't raising?

2012-01-12 Thread Fearless Fool
I have two blocks of tests: one for testing positive outcomes and one for testing negative. After poking around, I've concluded that the underlying :verify_credentials method is getting called, despite the stub: describe 'electricity credentials' do before(:each) do @wizard.electricit

[rspec-users] Rails3: setting session[:session_id] for requests testing

2011-05-09 Thread Fearless Fool
My app has some logic that permits some actions before the user logs in, and I depend upon session[:session_id] as a handle for some state. My requests testing fails because session[:session_id] is null. I attempted a somewhat brute force approach of putting this in my specs/requests/authenticati

Re: [rspec-users] ordering dependency in tests?

2011-02-24 Thread Fearless Fool
@Pat: thanks for the suggestions. It's perhaps not only what David suggested -- I seem to be suffering from some cached AR value. I changed my constant declaration to: RESIDENTIAL = find_or_create_by_name("residential") This keeps the number of instances down to a 1. That's good. But if I

Re: [rspec-users] ordering dependency in tests?

2011-02-24 Thread Fearless Fool
SOLVED -- it was an AR caching problem. I had Factory code that was essentially doing: def make_me_a_premise(opts = {}) opts = MODEL_PREMISE_DEFAULTS.merge(opts) premise = Factory(:premise) Factory(:premise_group_member, :premise => premise, :premise_group =>

Re: [rspec-users] ordering dependency in tests?

2011-02-24 Thread Fearless Fool
David Chelimsky wrote in post #983690: >>RESIDENTIAL = self.create(:name => "residential") > ^^ this is probably the problem ^^ > ... Ah! got it. FWIW, I wrote a query to the Rails group several months ago wondering if this construct was legit and got feedback that it was legit. Your expl

Re: [rspec-users] ordering dependency in tests?

2011-02-24 Thread Fearless Fool
yet more info... The two errors appear to be closely related. When run as individual files, PremiseGroup#premises is returning expected values. When run together, premise_group.premises returns "phantom" premises (which show up as nil values). For test B, the phantoms causes the count to b

Re: [rspec-users] ordering dependency in tests?

2011-02-24 Thread Fearless Fool
David Chelimsky wrote in post #983675: > On Feb 24, 2011, at 11:55 AM, Fearless Fool wrote: > What are the failures you're seeing? When running A before B, B's assertion that: @common_options[:premise_group].premises.size.should == 3 fails because premise_group.premises.size =

Re: [rspec-users] ordering dependency in tests?

2011-02-24 Thread Fearless Fool
A little additional information: every time I run the tests, it creates a new PremiseGroup model. Shouldn't the db get rolled back between tests? Here is PremiseGroup, listed here in its entirety: class PremiseGroup < ActiveRecord::Base has_many :premise_group_members, :dependent => :des

[rspec-users] ordering dependency in tests?

2011-02-24 Thread Fearless Fool
I'm baffled. If I do: $ bundle exec ruby -S rspec --tty A_spec.rb $ bundle exec ruby -S rspec --tty B_spec.rb I get no errors. But then if I do: $ bundle exec ruby -S rspec --tty A_spec.rb B_spec.rb I get an error on B_spec. And if I reverse the order: $ bundle exec ruby -S rspe

Re: [rspec-users] stubbing gets undone?

2011-02-16 Thread Fearless Fool
Scott Taylor wrote in post #981963: > Is #unstub being called at any point? > Scott No, but I think I see what's happening: I'm stubbing a particular (in-memory) instance of an AR. Later on in my code, the same AR is fetched, but now has a different in-memory address, and thus isn't subject to

Re: [rspec-users] stubbing gets undone?

2011-02-15 Thread Fearless Fool
Hmm - when I print puts(premise) rather than puts(premise.inspect), I can see that they actually have different addresses, so they're not really equal: just stubbed # entering lookup_stuff_on_the_web with # Now my problem is figuring out why/where the other Premise is getting created. In

[rspec-users] stubbing gets undone?

2011-02-15 Thread Fearless Fool
I'm looking at an example where a stub seems to work sometimes, and sometimes appears to become "unstubbed". I haven't boiled it down to a minimal example, but it goes something like this: the model: class Premise << ActiveRecord::Base def lookup_stuff_on_the_web $stderr.puts("entering

Re: [rspec-users] stubbing out after_create AR method?

2011-02-14 Thread Fearless Fool
Mike Mazur wrote in post #981665: > Perhaps one way to approach this is to have WebMock return an > appropriate success message in response to the network call that > :etl_attributes makes. > > Mike Ah - good point. With all the options for stubbing and mocking, I'd forgotten that one. Factory

[rspec-users] stubbing out after_create AR method?

2011-02-14 Thread Fearless Fool
I have a Premise model with an after_create method, along the lines of: class Premise < ActiveRecord::Base ... after_create :etl_attributes ... end In production code, etl_attributes accesses the web. For testing I'm using WebMock and VCR in cases where I want to test the etl_attributes fu

Re: [rspec-users] mocking an AR: ActiveRecord::AssociationTypeMismatch error

2011-02-12 Thread Fearless Fool
David Chelimsky wrote in post #981239: > http://relishapp.com/rspec/rspec-rails/v/2-5/dir/mocks/mock-model > HTH, > David Boy howdy, TH to the max. (How did I miss that???) Thanks. - ff -- Posted via http://www.ruby-forum.com/. ___ rspec-users maili

[rspec-users] mocking an AR: ActiveRecord::AssociationTypeMismatch error

2011-02-11 Thread Fearless Fool
I have a Premise model with lots of validations and somewhat complex callbacks. I've already written tests for those (and they pass). I also have a MeteredService model for which premise :has_many metered_services and (of course) metered_service :belongs_to premise. Testing MeteredService doesn'

Re: [rspec-users] [newbie] CSS navigation in controller response?

2011-01-25 Thread Fearless Fool
Evgeniy Dolzhenko wrote in post #977322: > You can pass a block to `have_selector` to nest your assertions, like: > ... > td is [#, ...] here Most wonderfully cool. If it's Nokogiri, then I'm on familiar turf. Thanks very much. - ff -- Posted via http://www.ruby-forum.com/. __

[rspec-users] [newbie] CSS navigation in controller response?

2011-01-24 Thread Fearless Fool
I'm hooked on RSpec after my first taste (thanks to http://railstutorial.org/book/ruby-on-rails-tutorial). And of course I have a newbish question. Assume a contrived doc structure like: moribund Now lets say I want to write an RSpec controller test that will pass if the status is