Hello,
Just to add to Raj's excellent advice, I feel a bit ambivalent about a lot
of the things written about so called agile development but with Ruby and
RSpec refactoring your way to a design is really possible. Be prepared to
massage your code to a good api-- that's why it's good to use git as you go.
Here are a couple of interesting links which helped me get started:
Jim Weirich - Roman Numerals Kata
There are two important things:
1. Write the test first ! (And get it to pass, refactor etc.)
2. Try and write at the method level -- pragmatic advice if you get stuck.
I find it useful to start with a constructor like:
RSpec.describe SomeModule::AClass do
describe '#initialize' do
it 'constructs a class' do
SomeModule::AClass.new(some, params)
end
end
end
And then you've already got some state, which you can think about. This is
cheating a bit because you can be wrong about the attributes a class needs,
but you have to be prepared to rewrite and change your code a lot and for
me this often works because I have some idea about what I want the class to
do (ie. not strictly test driven.)
In your program you might want to think about using exceptions and also
logging (ie. add a logger and error classes) to remove some of those error
messages from methods which are really doing other things, like checking
for the existence of a file.
Sometimes you don't need a class either. I'm not sure what you've got
planned here, but a module method might do for a configuration task?
Classes usually are nouns like WebServerManager, or something. This isn't
just convention it's a way to help think about object design. So, you might
have:
module ApacheServer
def self.daemon_exist?
File.exist? '/etc/init.d/httpd'
end
end
And then you might end up using this in the constructor?
module ApacheServer
class Manager
def initialize
# start_logging
raise DaemonNotFound.new("some important info") unless daemon.exist?
# init manager state? uptime, load_statiscs, etc..
end
. . .
end
end
There are some good bash scripts which do the task(s) you are programming.
It might be a good exercise to see how they work and see how you can do
that in ruby?
···
On Wed, Sep 2, 2015 at 2:37 AM, Robert Freiberger <rfreiberger@gmail.com> wrote:
Hello Ruby-lang,
I'm learning Rspec and Ruby which I heard is a good step since it's
developing TDD practice for real world use. While I understand the high
level point of view about using TDD with Rspec, I'm struggling to know how
to test certain conditions.
Below is my repo which I wrote a test for the first function but don't
believe this is covering the changes.
https://github.com/rfreiberger/Ruby-Rspec-Example
Is there suggestions on getting a proper start with Rspec or should I just
focus on Ruby before I learn unit testing?
Thanks,
Robert
--
joe gain
jacob-burckhardt-str. 16
78464 konstanz
germany
+49 (0)7531 60389
(...otherwise in ???)