How to include directories while debugging and testing a gem - ruby

Can anybody throw me a line?
I'm having problems in tests implementation, concretely, while requiring files.
I am developing a gem called mme_tools with a directory tree like this. The initial scaffold was generated using jeweler.
mme_tools
|-- examples
| |-- demo_enumerable.rb
| `-- demo_print_debug.rb
|-- Gemfile
|-- Gemfile.lock
|-- lib
| |-- mme_tools
| | |-- concurrent.rb
| | |-- debug.rb
| | |-- enumerable.rb
| | |-- version.rb
| | `-- webparse.rb
| `-- mme_tools.rb
|-- LICENSE.txt
|-- Rakefile
|-- README.rdoc
`-- test
`-- test_mme_tools.rb
where, mme_tools.rb is simply a container that requires all the files in mme_tools subdir (i.e. require 'mme_tools/debug.rb').
In the examples dir I can make some untidy tests (demos) as I progress. Simply putting a require 'mme_tools' at the top of each of those demos, and running ruby with option -I../lib works OK. In fact, I use Netbeans to develop so I include that option in every run.
My problem arises while testing. At the top of test_mme_tools.rb I have
require 'test/unit'
require 'mme_tools'
but my tests don't work because I think mme_tools.rb isn't found. The output from the rake task that I run from the shell is:
$ rake test
rake/rdoctask is deprecated. Use rdoc/task instead (in RDoc 2.4.2+)
/usr/bin/ruby1.8 -I"lib:lib:test" -I"/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib" "/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb" "test/**/test_*.rb"
Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader
Started
E
Finished in 0.02176 seconds.
1) Error:
test_compose(TC_MMETools):
NoMethodError: undefined method `compose' for MMETools::Enumerable:Module
/mnt/dropbox/DESENVOLUPAMENT/Gems/mme_tools/test/test_mme_tools.rb:19:in `test_compose'
1 tests, 0 assertions, 0 failures, 1 errors
rake aborted!
Command failed with status (1): [/usr/bin/ruby1.8 -I"lib:lib:test" -I"/usr/...]
Tasks: TOP => test
(See full trace by running task with --trace)
Of course, from Netbeans occurs the same, exceptuating that it shows the full trace
The file test_mme_tools.rb is as follows:
require 'test/unit'
require 'mme_tools'
class TC_MMETools < Test::Unit::TestCase
def setup
#a=(1..12)
#b=("A".."M")
#c=%w{tantmateix adhuc nogensmenys urgell pocassolta carrincló bajanada casundena Massalcoreig}
end
def test_compose
assert_equal [2,"B","adhuc"], MMETools::Enumerable.compose(#a,#b,#c)[1]
end
end
Any idea?

Assuming you continue with Rake, and update to use minitest, you can use a Rakefile that looks like this:
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << 'lib'
t.pattern = 'test/**/*.rb'
end

Related

Ginkgo package testing

I'm implementing test suite for brand new go app and decided to use ginkgo. The app has main function and several packages
.
|- main.go
|- types
| |-- user.go
| |-- post.go
|- server_pkg
| |-- users_controller.go
| |-- posts_controller.go
|- worker_pkg
| |-- users_worker.go
| |-- posts_worker.go
I ran ginkgo bootstrap in each package folder and added test files using ginkgo generate. Now I'm able to run tests for each package separately i.e.
cd server_pkg; ginkgo
The question is that: how to configure my application to run all tests for main function and packages using single command?
I can chain commands like ginkgo; cd server_pkg; ginkgo ..., but it does not look like good solution.
To run all test suits you should run this in command in your root catalog
ginkgo -r
Also is good practice as in normal test suits to run all test with race detector, also you could shuffle some test. You can run all this option by using
ginkgo -r --race --randomizeAllSpecs --randomizeSuites
#ttomalak thank you! It's exactly what I wanted
$ ginkgo -r

Can't create unittests IntelliJ

I am not able to select my testfolder in IntelliJ.
For some reason it wasn't there when I created this project, so I added it manually. After I did that, I went to project structure --> modules, where I marked it as a test.
I am simply not allowed to leave my source directory, when creating a testcase.
See image:
The problem doesn't occure when I create a new project, where the testfolder is set automaticly.
It seems that you have wrong project structure.
It should be as follows:
project-name
|-- pom.xml
|-- src
|-- main
| |-- java
| |-- resources
|-- test
|-- java
|-- resources
And don't mark the test directory as a test source root. You should mark the test/java (subdirectory under the test folder) instead:

Can I add factorygirl factories to shared gem

I have a set of active_model models I've extracted into a shared gem. I want the gem to also contain their factories for use in rspec tests defined in any of the projects that are using the gem to avoid defining the same factories in each project. Is that possible?
Yeah its possible. You can place the Factories inside your gem. Lets say lib/gem/factories/. Then your other gems can include the factory.
For example when you create the gem 'awesome_gem', you could have the followin structure:
|-- lib
| |-- awesome_gem
| | |-- module1
| | `-- version.rb
| |-- factories
| | |-- factory1.rb
| | `-- factory2.rb
| `-- awesome_gem.rb
|
`-- spec
|-- awesome_gem
`-- spec_helper.rb
Where lib contains your gem code and specs contain your specs.
awesome_gem.rb would define your gem includes such as awesome_gem/version.rb and more includes. However its recommended not to include the factories directory, since an user of your gem could not be using factory_girl.
Once the gem is packed, they can use require awesome_gem/factories/factory1.rb in their tests. Note that you need to specify your factory path in your spec helper to include your shared factories. Note that this solution is not tested, and paths that need to be included may be different. Also you can place the factories directory outside the lib dir, as long it is included by your .gemspec .

vanilla ruby rspec cannot see my files

So, I am trying to create a gem with a lib folder and a spec folder for rspec tests, like so:
- my_gem
|-- lib
| |-- my_gem
| | |-- foo.rb
| |
| |-- my_gem.rb
|
|-- spec
| |-- lib
| |-- foo_spec.rb
|
|-- my_gem.gemfile
|-- Rakefile
my_gem.rb contains require 'my_gem/foo' which works fine.
But my spec file which tries to require 'lib/my_gem' doesn't work. When I run rspec (or rake spec) without explicitly setting a path with -I ., it fails to load the file:
foo_spec.rb:1 in `require': cannot load such file -- lib/my_gem.rb (LoadError)
I feel like I shouldn't have to require a spec_helper.rb in every spec just to fix this, nor should I have to specify the load path for rspec every time. There has to be a simple convention that I am missing because I am new to gem development (I usually only do rails stuff).
Edit: I should point out that I am well aware that I can require_relative '../../lib/my_gem.rb'. The point is that I want to be able to get the root folder onto my path automatically like I am used to in rails. If this just isn't possible then please let me know.
The spec doesn't know the path to lib/my_gem. Try
require_relative '../../lib/my_gem'

Compass Source in Multiple Directories

Have you had success compiling SASS in multiple directories? Can you set up compass to recursively watch a directory?
I have read the documentation on add_import_path, but I would really appreciate some sample code, as I have (I am fairly certain) never written a line of ruby code.
The reason I ask is that I have several projects that share some standard scss. I would like changes to the shared scss to cascade to all projects.
thanks.
Let's say you have the following directroy structure:
project
|-- config.rb
+-- apps
|-- main.scss
|-- app1
+-- appst1.scss
|-- app2
+-- appst2.scss
+-- app3
+-- appst3.scss
Then adjust your config.rb:
sass_dir = "apps"
add_import_path "apps"
...
and in your main.scss include the other scss files:
#import "app1/appst1";
#import "app2/appst2";
#import "app3/appst3";
Here is my solution that supports batch compass compile/watch of multiple independent SASS projects, based on two Ruby scripts.
Folder structure with the Ruby files:
Root
--compile.rb
--watch.rb
--Module1
----config.rb
----css
----sass
--Module2
----config.rb
----css
----sass
--Module3
----config.rb
----css
----sass
Run compile.rb and watch.rb with several arguments representing the paths to your module folders containing the config.rb files.
I.e. : ruby compile.rb Module1/ Module2/ Module3/
compile.rb
require 'rubygems'
require 'compass'
require 'compass/exec'
ARGV.each do |arg|
Compass::Exec::SubCommandUI.new(["compile", arg, "--force"]).run!
end
I.e. : ruby watch.rb Module1/ Module2/ Module3/
watch.rb
require 'rubygems'
require 'compass'
require 'compass/exec'
threads = []
ARGV.each do |arg|
threads << Thread.new {
Compass::Exec::SubCommandUI.new(["watch", arg, "--force"]).run!
}
sleep(1)
end
threads.each { |thr| thr.join }
Notice that we need to create a separate thread for each compass watch (since they are blocking processes). sleep(1) is necessary because Compass::Exec::SubCommandUI is not actually thread-safe and might run several watches on the same module, instead of one on each. In case that happens, try increasing the sleep value.
Create a similar config.rb file in all modules. You might have to use compass init to get the a first config.rb that compass recognizes.
config.rb
http_path = "/"
css_dir = "css"
sass_dir = "sass"

Resources