Testing Rails application loading

10 views
Skip to first unread message

Nash Bridges

unread,
Feb 12, 2012, 11:39:43 AM2/12/12
to sporkgem
Hello everyone!
In order to run integration tests for a Rails engine I've created a
dummy Rails application. But such application can be initialized just
once within tests run scope, whereas I want to test different loading
configurations. Basically what I need is a fork before each scenario/
example. Is there a simple way to implement it with Spork?

Nash Bridges

unread,
Feb 14, 2012, 4:35:41 PM2/14/12
to sporkgem
I've managed to solve the problem this way:

require 'spork'
def run(*tests)
forker = Spork::Forker.new do
# require Rails dependencies only here

app = Class.new(Rails::Application)
# preconfigure dummy app if needed

yield(app.config) if block_given?
app.initialize!

results = tests.inject([]) do |memo, test|
memo << test.call
end
results.size == 1 ? results.first : results
end
forker.result
end

Then (assuming we included #run in RSpec examples group) we can:
it "should work" do
test = lambda { # your assertion goes here }
result = run test do |config|
# ...
end
result.should == # your expactations goes here
end

What's good is that you don't have to fire spork server at all. If
there's more elegant solution, please let me know.

Timothy Harper

unread,
Feb 12, 2012, 5:17:45 PM2/12/12
to spor...@googlegroups.com
Hi Nash,

I don't think you have much of a need of Spork for this. You can use some of the utility code that spork provides, such as Spork::Forker.

Several usage examples in the specs:

https://github.com/sporkrb/spork/blob/master/spec/spork/forker_spec.rb

The Forker class helps because while it runs the block you provide in a separate, forked process, the return value of the block is serialized via Marshal.dump and communicated back to the parent process over a mutually shared IO stream, allowing you to wait for the process to complete and retrieve it's result.

Tim

> --
> You received this message because you are subscribed to the Google Groups "sporkgem" group.
> To post to this group, send email to spor...@googlegroups.com.
> To unsubscribe from this group, send email to sporkgem+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sporkgem?hl=en.
>

Timothy Harper

unread,
Feb 16, 2012, 10:18:50 AM2/16/12
to spor...@googlegroups.com
The line "What's good is that you dot have to fire spork server at all" leads me to believe that you may not completely understand the mission spork is trying to fulfill. Spork is intended to tighten the feedback loop between modifying code and running applicable tests.

You may just want to extract that bit of code out since you're theoretically using a support library from Spork not entirely intended to be exposed for usage :) Or continue to require Spork just for the one class… either way, I'm glad you found the Forker class useful.

Tim

Nash Bridges

unread,
Feb 17, 2012, 8:40:23 AM2/17/12
to sporkgem
Hello, Tim!
I do understand that I've used (a part of) Spork in completely
opposite way it was designed :) At that moment I couldn't find a
solution how to provide clean environment before each test example,
then started playing with Spork and it turned out that Spork::Forker
fits my requirements very well. Thanks for this technique, it's still
blows my mind :)
Reply all
Reply to author
Forward
0 new messages