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 bothapp/andlib/. 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.
| 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 |
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/6238056a-ac03-4b60-98f4-8cf71b71308f%40googlegroups.com.