Need to do Unit testing for a gem using RSPEC framework

28 views
Skip to first unread message

Venkata Avinash Duggirala

unread,
Sep 18, 2014, 8:08:33 AM9/18/14
to rs...@googlegroups.com
Hi,

I need to do UNIT Testing to a gem which is in development. I want to use RSPEC framework. I am well versed in using RSPEC framework to automate web application.

But, I never created a project to do unit testing a gem. Can anyone provide me how to start up, setting up the project, how folder structure should.

Awaiting reply.

Regards,
Avinash Duggirala

Aaron Kromer

unread,
Sep 18, 2014, 11:50:54 AM9/18/14
to rs...@googlegroups.com

Avinash,

Glad to hear you like RSpec. Setting up a gem project is fairly similar. Just start your gem, add rspec as a development dependency and run the rspec setup: rspec --init. That will create the .rspec and spec/spec_helper.rb files for you. After that it’s just as normal. The folder structure is up to you. In general, my suggestion is to mirror your lib directory. This is in the rspec-rails docs, though it probably can be added to the main rspec docs too. I’ve copied and adjusted the doc to remove the rails-y part:

It is suggested that the spec/ directory structure generally mirror both
app/ and lib/. This makes it easy to locate corresponding code and spec
files.

Example:

 lib
 ├── country_map
 │   ├── city.rb
 │   └── state.rb
 ├── country_map.rb
 └── tasks
     ├── irc.rake
 spec
 ├── country_map
 │   ├── city_spec.rb
 │   └── state_spec.rb
 ├── country_map_spec.rb
 ├── spec_helper.rb
 ├── support
 │   ├── allow_remote_requests.rb
 │   ├── custom_matchers.rb
 └── tasks
     ├── irc_spec.rb

Happy specing


--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/8271cc91-6d84-458e-8329-e2416dd61c86%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Venkata Avinash Duggirala

unread,
Sep 19, 2014, 2:34:04 AM9/19/14
to rs...@googlegroups.com
Thanks a lot Aron. Successfully created the folder structure 

One more step of suggestion needed.  Below is the sample class I need to test, can you guide me how to create tests that validates the functions etc and how to do test coverage? 


module Nucleon
class Codes
#-----------------------------------------------------------------------------
# Code index
@@registry = {}
@@status_index = {}
@@next_code = 0
#---
def self.registry
@@registry
end
#---
def self.index(status_code = nil)
if status_code.nil? || ! status_code.integer?
@@status_index
else
status_code = status_code.to_i
if @@status_index.has_key?(status_code)
@@status_index[status_code]
else
@@status_index[registry[:unknown_status]]
end
end
end
#---
def self.render_index(status_code = nil)
output = "Status index:\n"
@@status_index.each do |code, name|
name = name.gsub(/_/, ' ').capitalize
if ! status_code.nil? && status_code == code
output << sprintf(" [ %3i ] - %s\n", code, name)
else
output << sprintf(" %3i - %s\n", code, name)
end
end
output << "\n"
output
end
#-----------------------------------------------------------------------------
# Code construction
def self.code(name)
name = name.to_sym
unless registry.has_key?(name)
status_code = @@next_code
@@next_code = @@next_code + 1
# TODO: Add more information to the index (like a help message)
@@registry[name] = status_code
@@status_index[status_code] = name.to_s
end
end
#---
def self.codes(*codes)
codes.each do |name|
code(name)
end
end
#-----------------------------------------------------------------------------
# Return status codes on demand
def [](name)
name = name.to_sym
if @@registry.has_key?(name)
@@registry[name]
else
@@registry[:unknown_status]
end
end
#---
def method_missing(method, *args, &block)
self[method]
end
#-----------------------------------------------------------------------------
# Core status codes
code(:success) # This must be added first (needs to be 0)
code(:help_wanted)
code(:unknown_status)
code(:action_unprocessed)
code(:batch_error)
code(:validation_failed)
code(:access_denied)
end
end

Thanks in advance .

Regards,
Avinash Duggirala
Skype:avinash5302 

Aaron Kromer

unread,
Sep 19, 2014, 10:35:56 AM9/19/14
to rs...@googlegroups.com

I don’t have any specifics to provide other than, I would setup my world state (if any) as context blocks. Then inside each context, I would multiple specs to test each method setting, usually, one expectation per spec on either the return value, or a side-effect. For side-effects, if it changed the object under test, I would then try to look at a public API to confirm this. If the object worked with a collaborator, I would weigh if it’s more beneficial or makes more sense to use a real collaborator and then inspect its state. Or if it makes more sense to inject a spy and assert that the appropriate command action was called on it.


Reply all
Reply to author
Forward
0 new messages