Re: [rspec-users] Noob syntax questions regarding rspec book...

2009-07-07 Thread ssmithstone
I to am in the same situation and the meaning of the /^ $/ is a
regular expression so it matches yours features so in your features is
you do

Give some random text I guess some more random test

the code would run against that given method, hope that makes sense
and that's my view of the code

On Jul 7, 4:35 pm, Chris Sund  wrote:
> Hey Everyone,
>
> I've been working my way through the Rspec book trying to absorb and
> understand everything. This is my first time with BDD and I'm just
> trying to figure out some simple syntax stuff. My questions revolve
> around some of the syntaxing used in the book. These are really simple
> questions.
>
> 1.) Given /^the secret code is (. . . .)$/ do |code|
>         Is (. . . .) simply a place holder? could I use something like
> (- - - -) instead, or does it actually mean something?
>
> 2.) Then /^the mark should be (.*)$/ do |mark|
>         Similar questionwhat does .* represent?
>
> 3.) In the following example why don't I pass   |guess| to the When
> statement? I'm sure it has something to do with the (code.split)
> syntax, I'm just not sure what.
>
> When /^I guess (. . . .)$/ do |code|
> @game.guess(code.split)
> end
>
> 4.) And finally what does ("\n") do?
>
> Then /^the mark should be (.*)$/ do |mark|
>   @messenger.string.split("\n").should include(mark)
> end
>
> Thank You!
>
> Chris
> ___
> rspec-users mailing list
> rspec-us...@rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Noob syntax questions regarding rspec book...

2009-07-07 Thread Ben Mabey

Rick DeNatale wrote:

On Tue, Jul 7, 2009 at 11:35 AM, Chris
Sund wrote:
  

Hey Everyone,

I've been working my way through the Rspec book trying to absorb and
understand everything. This is my first time with BDD and I'm just
trying to figure out some simple syntax stuff. My questions revolve
around some of the syntaxing used in the book. These are really simple
questions.


1.) Given /^the secret code is (. . . .)$/ do |code|
   Is (. . . .) simply a place holder? could I use something like
(- - - -) instead, or does it actually mean something?



Yes it actually means something. the stuff between // is a regular
expression, and some of the characters have meaning.

   ^ means the beginning of the string
   $ means the end of the string
   Each . will mark a single character, any character will do.
  The parens mark a group, the part of the string which marks the
group will be assigned to the code parameter.
  So when the whole regex matches code will be set to the four
characters of the code separated by spaces.
  

2.) Then /^the mark should be (.*)$/ do |mark|
   Similar questionwhat does .* represent?



it means zero or more arbitrary characters

  

3.) In the following example why don't I pass   |guess| to the When
statement? I'm sure it has something to do with the (code.split)
syntax, I'm just not sure what.

When /^I guess (. . . .)$/ do |code|
@game.guess(code.split)
end



There isn't a variable named guess here. As I said in answer to the
first question, if the story says

When I guess 1 3 4 2

then when the step is executed the code parameter to the block will be
set to "1 3 4 2" and "1 3 4 2".split gives ["1", "3", "4", "2"]
  

4.) And finally what does ("\n") do?

Then /^the mark should be (.*)$/ do |mark|
 @messenger.string.split("\n").should include(mark)
end



"\n" is a ruby string literal representing a new-line, so
   @messenger.string.split("\n") results in an array comprising each
line within @messenger.string



  



Chris,
FYI, a good resource to learn and play around with reg exps in ruby is: 
http://rubular.com/


-Ben
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Noob syntax questions regarding rspec book...

2009-07-07 Thread Rick DeNatale
On Tue, Jul 7, 2009 at 11:35 AM, Chris
Sund wrote:
> Hey Everyone,
>
> I've been working my way through the Rspec book trying to absorb and
> understand everything. This is my first time with BDD and I'm just
> trying to figure out some simple syntax stuff. My questions revolve
> around some of the syntaxing used in the book. These are really simple
> questions.
>
>
> 1.) Given /^the secret code is (. . . .)$/ do |code|
>        Is (. . . .) simply a place holder? could I use something like
> (- - - -) instead, or does it actually mean something?

Yes it actually means something. the stuff between // is a regular
expression, and some of the characters have meaning.

   ^ means the beginning of the string
   $ means the end of the string
   Each . will mark a single character, any character will do.
  The parens mark a group, the part of the string which marks the
group will be assigned to the code parameter.
  So when the whole regex matches code will be set to the four
characters of the code separated by spaces.
>
> 2.) Then /^the mark should be (.*)$/ do |mark|
>        Similar questionwhat does .* represent?

it means zero or more arbitrary characters

> 3.) In the following example why don't I pass   |guess| to the When
> statement? I'm sure it has something to do with the (code.split)
> syntax, I'm just not sure what.
>
> When /^I guess (. . . .)$/ do |code|
> @game.guess(code.split)
> end

There isn't a variable named guess here. As I said in answer to the
first question, if the story says

When I guess 1 3 4 2

then when the step is executed the code parameter to the block will be
set to "1 3 4 2" and "1 3 4 2".split gives ["1", "3", "4", "2"]
> 4.) And finally what does ("\n") do?
>
> Then /^the mark should be (.*)$/ do |mark|
> �...@messenger.string.split("\n").should include(mark)
> end

"\n" is a ruby string literal representing a new-line, so
   @messenger.string.split("\n") results in an array comprising each
line within @messenger.string



-- 
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Noob syntax questions regarding rspec book...

2009-07-07 Thread David Chelimsky
On Tue, Jul 7, 2009 at 10:35 AM, Chris
Sund wrote:
> Hey Everyone,
>
> I've been working my way through the Rspec book trying to absorb and
> understand everything. This is my first time with BDD and I'm just
> trying to figure out some simple syntax stuff. My questions revolve
> around some of the syntaxing used in the book. These are really simple
> questions.

Hey Chris,

The book assumes a basic working knowledge of Ruby. The questions you
are asking are about Ruby, not about RSpec or Cucumber. I'll answer
them for you here, but I don't think this is appropriate or necessary
for the book itself.

> 1.) Given /^the secret code is (. . . .)$/ do |code|
>        Is (. . . .) simply a place holder? could I use something like
> (- - - -) instead, or does it actually mean something?

The argument to Given is a regular expression. The "." means "any
character", so (. . . .) means any four characters with spaces in
between. There are certainly more specific and fool-proof ways we
could express this, but this is a very simple way, and works just fine
in the context of a cucumber step definition.

> 2.) Then /^the mark should be (.*)$/ do |mark|
>        Similar questionwhat does .* represent?

The "." means any character and the "*" means any number of times.

> 3.) In the following example why don't I pass   |guess| to the When
> statement? I'm sure it has something to do with the (code.split)
> syntax, I'm just not sure what.
>
> When /^I guess (. . . .)$/ do |code|
> @game.guess(code.split)
> end

You're correct - code.split converts "r y g c" to ['r', 'y', 'g',
'c'], which is what the game wants to receive (an array, rather than a
string).

> 4.) And finally what does ("\n") do?
>
> Then /^the mark should be (.*)$/ do |mark|
> �...@messenger.string.split("\n").should include(mark)
> end

The @messenger is a StringIO object. It receives puts() statements and
adds them to it's string attribute with a line break ("\n") at the end
so if you do this:

@messenger.puts "a"
@messenger.puts "b"

Then the result of @messenger.string is "a\nb\n"

Splitting that on "\n" results in ["a","b"], which allows you to ask
if that result includes "a", for example:

  @messenger.split("\n").include?("a")

Which can be expressed as an expectation in RSpec like this:

  @messenger.split("\n").should include("a")


HTH,
David

>
>
>
>
>
> Thank You!
>
> Chris
> ___
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Noob syntax questions regarding rspec book...

2009-07-07 Thread Yi Wen

They are regex.
On Jul 7, 2009, at 10:35 AM, Chris Sund wrote:


Hey Everyone,

I've been working my way through the Rspec book trying to absorb and
understand everything. This is my first time with BDD and I'm just
trying to figure out some simple syntax stuff. My questions revolve
around some of the syntaxing used in the book. These are really simple
questions.


1.) Given /^the secret code is (. . . .)$/ do |code|
   Is (. . . .) simply a place holder? could I use something like
(- - - -) instead, or does it actually mean something?

2.) Then /^the mark should be (.*)$/ do |mark|
   Similar questionwhat does .* represent?



3.) In the following example why don't I pass   |guess| to the When
statement? I'm sure it has something to do with the (code.split)
syntax, I'm just not sure what.

When /^I guess (. . . .)$/ do |code|
@game.guess(code.split)
end


4.) And finally what does ("\n") do?

Then /^the mark should be (.*)$/ do |mark|
 @messenger.string.split("\n").should include(mark)
end





Thank You!

Chris
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


[rspec-users] Noob syntax questions regarding rspec book...

2009-07-07 Thread Chris Sund
Hey Everyone,

I've been working my way through the Rspec book trying to absorb and
understand everything. This is my first time with BDD and I'm just
trying to figure out some simple syntax stuff. My questions revolve
around some of the syntaxing used in the book. These are really simple
questions.


1.) Given /^the secret code is (. . . .)$/ do |code|
Is (. . . .) simply a place holder? could I use something like
(- - - -) instead, or does it actually mean something?

2.) Then /^the mark should be (.*)$/ do |mark|
Similar questionwhat does .* represent?



3.) In the following example why don't I pass   |guess| to the When
statement? I'm sure it has something to do with the (code.split)
syntax, I'm just not sure what.

When /^I guess (. . . .)$/ do |code|
@game.guess(code.split)
end


4.) And finally what does ("\n") do?

Then /^the mark should be (.*)$/ do |mark|
  @messenger.string.split("\n").should include(mark)
end





Thank You!

Chris
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] should_receive with a block

2009-07-07 Thread doug livesey
Hi -- cheers for that. I've taken your advice (the 1st approach).
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] [Rails] Controller testing problem

2009-07-07 Thread Zach Moazeni

Bleh,

I forgot to mention, if you have to maintain controller action code  
that is dependent on instance variables set elsewhere, here is a ruby  
hack that will allow you to setup the context. (Please note, I  
recommend refactoring the code under test but I understand sometimes  
the preconditions of a test need to be a littleslimy. However,  
both the code and the spec are smelly)


require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require File.expand_path(File.dirname(__FILE__) + '/../spec_helpers/ 
controller_behaviors')


describe SomeController do
  stub_all_filters!

  describe "#foo" do
def do_get
  get :foo
end

it "should do something" do
  controller.send(:instance_variable_set, "@bar", "here's bar")
  do_get
end
  end
end

class SomeController < ApplicationController
  def foo
p "need to do something with #...@bar}"
  end
end

Here's a gist in case the code formatting goes awry http://gist.github.com/142050 
 .


On Jul 7, 2009, at 8:29 AM, Zach Moazeni wrote:


Hey Ignacy,

I think I'm in the minority on this one, but I tend to keep my  
action logic independent of filters. Doing that, my typical  
controller spec begins like this:


require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require File.expand_path(File.dirname(__FILE__) + '/../spec_helpers/ 
controller_behaviors')


describe SomeController do
 stub_all_filters!

 describe ...

 ...
end

The method "stub_all_filters!" is a custom rolled, per project  
maintained, rspec hack that rips through Rails's internal methods to  
find the filters on a controller and creates a "before do ... end"  
that stubs each filter to "true" [1]


I still use before filters because it's a good fit for many plugins  
such as ssl_requirement and authentication. However 1) I don't  
explicitly split up controller action behavior into filters and 2)  
the controller actions can depend on the filters indirectly by  
consuming methods like "self.current_user"


I know you're maintaining prior-written code, but I thought this  
would be relevant. I hope this helps more than it confuses.


[1] - http://gist.github.com/142047


--
Zach Moazeni
http://simplechatter.com



___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] [Rails] Controller testing problem

2009-07-07 Thread Zach Moazeni

Hey Ignacy,

I think I'm in the minority on this one, but I tend to keep my action  
logic independent of filters. Doing that, my typical controller spec  
begins like this:


require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require File.expand_path(File.dirname(__FILE__) + '/../spec_helpers/ 
controller_behaviors')


describe SomeController do
  stub_all_filters!

  describe ...

  ...
end

The method "stub_all_filters!" is a custom rolled, per project  
maintained, rspec hack that rips through Rails's internal methods to  
find the filters on a controller and creates a "before do ... end"  
that stubs each filter to "true" [1]


I still use before filters because it's a good fit for many plugins  
such as ssl_requirement and authentication. However 1) I don't  
explicitly split up controller action behavior into filters and 2) the  
controller actions can depend on the filters indirectly by consuming  
methods like "self.current_user"


I know you're maintaining prior-written code, but I thought this would  
be relevant. I hope this helps more than it confuses.


[1] - http://gist.github.com/142047

On Jul 1, 2009, at 4:18 AM, Ignacy Moryc wrote:


Hi,

I'm working on a legacy app - it was created in 1.2.5 then over time
ported to 2.3. It has absolutely no tests - and I'm trying to fix that
in between adding new functionality.

My problem right now is that my ApplicationController is running
before_filters for setting different request variables (like current
location, etc.) and this messes up my tests - I can't test responses
properly (they are all redirects because ApplicationController derives
from SiteController which actions implement setting variables). What's
worse I can't test assigns hash - it doesn't get proper values  
assigned.


Is there a way for RSpec to skip the filters in tests? Or to stop
ApplicationController to derive from SiteController?

Thanks,
Ignacy


--
Zach Moazeni
http://simplechatter.com



___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] should_receive with a block

2009-07-07 Thread Zach Moazeni

Hey Doug,

That won't work because of how Ruby handles the equality of lambdas/ 
procs.


From my experience there are 2 approaches.

1) Test both the "call" and the block together. So instead of mocking  
Util, mock SfProperty.


or

2) Store the lambda somewhere (like a Class variable). Test the lambda  
separately, and test the "call" separately, but treat the lambda as a  
black box. (though I'm sure you could do something funky with  
redefining the Class variable within the test, and asserting that the  
redefinition got passed in the "call" method).


Testing passed lambdas/procs can be tricky because you're testing the  
behavior of something that _defines behavior_ and passes it to  
something else.


On Jul 7, 2009, at 7:27 AM, doug livesey wrote:


Hi -- can I specify the block that should be called with a method?
Sort of like this:

it "should bulk update the properties_params in groups of 50" do
  block = lambda do |params|
SfProperty.put( :bulk_update, :sf_properties => params )
  end

   
Util.should_receive( :do_to_in_groups_of ).with( @properties_params,  
50, &block )


  do_call
end

Or am I dreaming?
Cheers,
   Doug.
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


--
Zach Moazeni
http://simplechatter.com



___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


[rspec-users] should_receive with a block

2009-07-07 Thread doug livesey
Hi -- can I specify the block that should be called with a method?
Sort of like this:

it "should bulk update the properties_params in groups of 50" do
  block = lambda do |params|
SfProperty.put( :bulk_update, :sf_properties => params )
  end

  Util.should_receive( :do_to_in_groups_of ).with( @properties_params,
50, &block )

  do_call
end

Or am I dreaming?
Cheers,
   Doug.
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users