Learning TDD Rspec

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

Hi Robert,
There are different perspectives on your second question and I wouldn't presume to have all the answers there. In my opinion, knowing how to write some basic tests is good because you give yourself the ability to make changes in your code and check for regressions easily. Writing in-depth tests is a different sort of problem and you should feel free to research that area.

For your code specifically, I have the following comments:
  1) You instantiate your Report object in your library file. This is generally bad practice. Your library should simply declare/define the object and you should allow the user that requires your library to instantiate the object on their own, when they want to.
    2) The same concept goes for your validation. You call your validation method in the object initializer but you should simply initialize your state variables and then allow the user to instantiate the object on their own and then call validation when they are ready.

  3) Once you fix the above points, you can then instantiate a single object in your spec test and test the 3 methods (check/restart/validate) separately, which is the main strength of unit tests (think divide and conquer).

  4) Additionally, you are testing something that uses system calls to achieve results and this can be tricky for a beginner to test (but it is possible). You are trying to inject an initial condition to cause failure, but you are not taking the scoping of the object into account so therefore you don't get the desired result. You might want to consider abstracting out the system interaction so you can test the
  method logic separately.

···

On 09/02/2015 00:37:51, Robert Freiberger 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

--
Raj Sahae
408.230.8531

Hi Rubyists,

I am new to Ruby. Appreciate very much if you could introduce to me a good
textbook for beginner.
Thanks,
Phuong

···

On Wed, Sep 2, 2015 at 10:09 AM Raj Sahae <rajsahae@gmail.com> wrote:

Hi Robert,
There are different perspectives on your second question and I wouldn't
presume to have all the answers there. In my opinion, knowing how to
write some basic tests is good because you give yourself the ability to
make changes in your code and check for regressions easily. Writing
in-depth tests is a different sort of problem and you should feel free
to research that area.

For your code specifically, I have the following comments:
  1) You instantiate your Report object in your library file. This is
  generally bad practice. Your library should simply declare/define the
  object and you should allow the user that requires your library to
  instantiate the object on their own, when they want to.

  2) The same concept goes for your validation. You call your validation
  method in the object initializer but you should simply initialize your
  state variables and then allow the user to instantiate the object on
  their own and then call validation when they are ready.

  3) Once you fix the above points, you can then instantiate a single
  object in your spec test and test the 3 methods
  (check/restart/validate) separately, which is the main strength of
  unit tests (think divide and conquer).

  4) Additionally, you are testing something that uses system calls to
  achieve results and this can be tricky for a beginner to test (but it
  is possible). You are trying to inject an initial condition to cause
  failure, but you are not taking the scoping of the object into account
  so therefore you don't get the desired result. You might want to
  consider abstracting out the system interaction so you can test the
  method logic separately.

On 09/02/2015 00:37:51, Robert Freiberger 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

--
Raj Sahae
408.230.8531

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 ???)

Hi, I'm learning rspec with rails and found this book... I think it
could be useful https://leanpub.com/everydayrailsrspec

hava a nice day :slight_smile:

···

On 02/09/15 02:54, Joe Gain wrote:

On Wed, Sep 2, 2015 at 2:37 AM, Robert Freiberger > <rfreiberger@gmail.com <mailto: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

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:
http://designisrefactoring.com/

Jim Weirich - Roman Numerals Kata
https://www.youtube.com/watch?v=983zk0eqYLY

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?
--
joe gain

jacob-burckhardt-str. 16
78464 konstanz
germany

+49 (0)7531 60389

(...otherwise in ???)

I'd suggest a few free online resources first. Learn Ruby The Hard way is good. Ruby Monk as well.

As for an actual book, the Well Grounded Rubyist has been pretty good. I'm Still working through it, but it's highly regarded, and one of the few that covers Ruby 2.0.

Best,

Danny

···

On Sep 2, 2015, at 12:48 AM, Phuong Phan <p.h.phan2006@gmail.com> wrote:

Hi Rubyists,

I am new to Ruby. Appreciate very much if you could introduce to me a good textbook for beginner.
Thanks,
Phuong

On Wed, Sep 2, 2015 at 10:09 AM Raj Sahae <rajsahae@gmail.com> wrote:
Hi Robert,
There are different perspectives on your second question and I wouldn't
presume to have all the answers there. In my opinion, knowing how to
write some basic tests is good because you give yourself the ability to
make changes in your code and check for regressions easily. Writing
in-depth tests is a different sort of problem and you should feel free
to research that area.

For your code specifically, I have the following comments:
  1) You instantiate your Report object in your library file. This is
  generally bad practice. Your library should simply declare/define the
  object and you should allow the user that requires your library to
  instantiate the object on their own, when they want to.

  2) The same concept goes for your validation. You call your validation
  method in the object initializer but you should simply initialize your
  state variables and then allow the user to instantiate the object on
  their own and then call validation when they are ready.

  3) Once you fix the above points, you can then instantiate a single
  object in your spec test and test the 3 methods
  (check/restart/validate) separately, which is the main strength of
  unit tests (think divide and conquer).

  4) Additionally, you are testing something that uses system calls to
  achieve results and this can be tricky for a beginner to test (but it
  is possible). You are trying to inject an initial condition to cause
  failure, but you are not taking the scoping of the object into account
  so therefore you don't get the desired result. You might want to
  consider abstracting out the system interaction so you can test the
  method logic separately.

On 09/02/2015 00:37:51, Robert Freiberger 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

--
Raj Sahae
408.230.8531

Thanks everyone,

I'm taking the feedback and working on improving my core Ruby skills. Also
reading up on the testing. I have a copy of Well Grounded Rubyist, which I
haven't completed yet.

Again thank you,
Robert

···

On Wed, Sep 2, 2015 at 5:25 AM Daniel Llinas <danllinas@gmail.com> wrote:

I'd suggest a few free online resources first. Learn Ruby The Hard way is
good. Ruby Monk as well.

As for an actual book, the Well Grounded Rubyist has been pretty good. I'm
Still working through it, but it's highly regarded, and one of the few that
covers Ruby 2.0.

Best,

Danny

http://www.wired.com/2015/09/facebooks-new-anti-spam-system-hints-future-coding/

On Sep 2, 2015, at 12:48 AM, Phuong Phan <p.h.phan2006@gmail.com> wrote:

Hi Rubyists,

I am new to Ruby. Appreciate very much if you could introduce to me a good
textbook for beginner.
Thanks,
Phuong

On Wed, Sep 2, 2015 at 10:09 AM Raj Sahae <rajsahae@gmail.com> wrote:

Hi Robert,
There are different perspectives on your second question and I wouldn't
presume to have all the answers there. In my opinion, knowing how to
write some basic tests is good because you give yourself the ability to
make changes in your code and check for regressions easily. Writing
in-depth tests is a different sort of problem and you should feel free
to research that area.

For your code specifically, I have the following comments:
  1) You instantiate your Report object in your library file. This is
  generally bad practice. Your library should simply declare/define the
  object and you should allow the user that requires your library to
  instantiate the object on their own, when they want to.

  2) The same concept goes for your validation. You call your validation
  method in the object initializer but you should simply initialize your
  state variables and then allow the user to instantiate the object on
  their own and then call validation when they are ready.

  3) Once you fix the above points, you can then instantiate a single
  object in your spec test and test the 3 methods
  (check/restart/validate) separately, which is the main strength of
  unit tests (think divide and conquer).

  4) Additionally, you are testing something that uses system calls to
  achieve results and this can be tricky for a beginner to test (but it
  is possible). You are trying to inject an initial condition to cause
  failure, but you are not taking the scoping of the object into account
  so therefore you don't get the desired result. You might want to
  consider abstracting out the system interaction so you can test the
  method logic separately.

On 09/02/2015 00:37:51, Robert Freiberger 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

--
Raj Sahae
408.230.8531