Forum: RSpec STDIN gets method issue with rspec

Posted by Gokul Murthy (gokulamurthy)
on 2010-01-22 21:13
(Received via mailing list)
Hi,

I am new to rspec. I am trying to write a spec for a simple ruby program 
i.e

class First
def test
a = gets.chomp
return a
end
end

I have tried in different ways test method is called from the spec file. 
but
gets.chomp is not executing…

Please kindly advise me, how to write spec for this.

Thanks
Gokul
Posted by Gokul Murthy (gokulamurthy)
on 2010-01-22 21:19
(Received via mailing list)
Hi,

I am new to rspec. I am trying to write a spec for a simple ruby program 
i.e

class First
def test
a = gets.chomp
return a
end
end

I have tried in different ways test method is called from the spec file. 
but
gets.chomp is not executing…

Please kindly advise me, how to write spec for this.

Thanks
Gokul
Posted by David Chelimsky (Guest)
on 2010-01-23 13:38
(Received via mailing list)
On Fri, Jan 22, 2010 at 1:46 PM, gokul murthy <railsthinker@gmail.com> 
wrote:
> Please kindly advise me, how to write spec for this.
Even though it seems simple, you've chosen a rather complex situation
to try to learn Rspec from. Did you pick this because you really need
to solve it, or is this just an academic exercise? If the latter,
let's start somewhere simpler, and get to things like simulating IO
later :)

It is always easiest to start with examples in which you can create an
object with some state, send it a message (call a method), and set
expectations about the result: given/when/then.

describe Person, "full name" do
  context "with a first and last name" do
    it "concats the names with a space between them" do
      #given
      person = Person.new(:first_name => "Gokul", :last_name => 
"Murthy")
      # when
      full_name = person.full_name
      #then
      full_name.should == "Gokul Murthy"
    end
  end
end

I separated out the When and Then to make it clear which is which, but
in practice I'd likely just write this:

    it "concats the names with a space between them" do
      person = Person.new(:first_name => "Gokul", :last_name => 
"Murthy")
      person.full_name.should == "Gokul Murthy"
    end

Now we have a pretty clear picture of how we want to create a Person
and how we want to ask for its name, and what our expected outcome is.

If you really want to know about specifying IO, let me know and I'll
show you a couple of ways to do it, but they are much less
straightforward.

Cheers,
David
Posted by Tumas Bajoras (hanniph)
on 2010-02-28 22:53
David Chelimsky wrote:
> On Fri, Jan 22, 2010 at 1:46 PM, gokul murthy <railsthinker@gmail.com> 
> wrote:
>> Please kindly advise me, how to write spec for this.
> Even though it seems simple, you've chosen a rather complex situation
> to try to learn Rspec from. Did you pick this because you really need
> to solve it, or is this just an academic exercise? If the latter,
> let's start somewhere simpler, and get to things like simulating IO
> later :)
> 
> It is always easiest to start with examples in which you can create an
> object with some state, send it a message (call a method), and set
> expectations about the result: given/when/then.
> 
> describe Person, "full name" do
>   context "with a first and last name" do
>     it "concats the names with a space between them" do
>       #given
>       person = Person.new(:first_name => "Gokul", :last_name => 
> "Murthy")
>       # when
>       full_name = person.full_name
>       #then
>       full_name.should == "Gokul Murthy"
>     end
>   end
> end
> 
> I separated out the When and Then to make it clear which is which, but
> in practice I'd likely just write this:
> 
>     it "concats the names with a space between them" do
>       person = Person.new(:first_name => "Gokul", :last_name => 
> "Murthy")
>       person.full_name.should == "Gokul Murthy"
>     end
> 
> Now we have a pretty clear picture of how we want to create a Person
> and how we want to ask for its name, and what our expected outcome is.
> 
> If you really want to know about specifying IO, let me know and I'll
> show you a couple of ways to do it, but they are much less
> straightforward.
> 
> Cheers,
> David

Hey,

I am learning RSpec as well and it would be great if you could show how 
to test IO.

Thanks,
Tumas
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.