Re: [rspec-users] Can you explain this code to me?

2008-02-07 Thread Jarkko Laine
On 8.2.2008, at 1.26, Pito Salas wrote:

> I am experimenting with the restful-authentication plug in and I see
> that it has generated an rspec encantation which I had not seen
> before:
>
>  it 'allows signup' do
>lambda do
>  create_user
>  response.should be_redirect
>end.should change(User, :count).by(1)
>  end
>
> What does the combination of a lambda and a .should mean?

Lambda is an anonymous function, or pretty much the same as a block of  
code. So in the case above, running the code inside the lambda block  
should change the user count by one. This is the same as  
assert_difference does in test/unit.

>
>
> Similarly but a little different:
>
>  describe 'being created' do
>before do
>  @user = nil
>  @creating_user = lambda do
>@user = create_user
>violated "[EMAIL PROTECTED]" if
> @user.new_record?
>  end
>end
>
>it 'increments User#count' do
>  @creating_user.should change(User, :count).by(1)
>end
>  end
>
> Thanks for any pointers or clarifications!

Same thing, just this time the function is first stored in an instance  
variable.

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


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


[rspec-users] Shared Examples + “initialized c onstant” warnings

2008-02-07 Thread Gerrit Kaiser
Hi List,
I'm using rspec's shared example groups in a rails project (rails  
2.02, rspec(on_rails) 1.1.3) and I'm getting lots of errors of the  
following kind when(before) running the specs:

(RAILS_ROOT)/vendor/plugins/rspec/lib/spec/extensions/main.rb:78:  
warning: already initialized constant ContentExamples
(RAILS_ROOT)/vendor/plugins/rspec/lib/spec/extensions/main.rb:78:  
warning: already initialized constant EditViewExamples

The shared example groups are defined in files like spec/shared/ 
foo_examples.rb
and declared as
share_as :FooExamples do…end
(and then included in other example groups with "include FooExamples")
They're loaded in spec_helper.rb with the following:
Dir.glob(File.join(File.dirname(__FILE__), 'shared', '*.rb')).each do | 
shared_example|
   require shared_example
end


I'm guessing that's what needs to change. Has anyone else seen these  
warnings? I find them quite annoying and would like to get rid of  
them. Any pointers would be much appreciated.
Thanks,
Gerrit
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Can you explain this code to me?

2008-02-07 Thread Pat Maddox
On Feb 7, 2008 3:40 PM, Bira <[EMAIL PROTECTED]> wrote:
> On Feb 7, 2008 9:26 PM, Pito Salas <[EMAIL PROTECTED]> wrote:
>
> > What does the combination of a lambda and a .should mean?
>
> I'm new to the list, but I think I can answer that :).
>
> >From what I know, it means that RSpec will run the lambda's code and
> check that it had the effect specified in the "should" call.
>
> And from that I can determine by looking at the two incantations you
> posted, the lambda is used because the code can throw an error, and
> the spec's author didn't want the first error to prevent the outer
> "should" from being checked.

Sometimes, but in this case, what you are trying to do is

before_count = User.send :count
create_user
response.should be_redirect
User.send(:count).should == before_count + 1

that's some boilerplate that can be abstracted away...

def change(target, method)
  before_count = target.send method
  yield
  target.send(method).should == before_count + 1
end

(not exactly how it's implemented, but it should give you a rough idea
of what's going on)

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


Re: [rspec-users] Can you explain this code to me?

2008-02-07 Thread David Chelimsky
On Feb 7, 2008 5:26 PM, Pito Salas <[EMAIL PROTECTED]> wrote:
> I am experimenting with the restful-authentication plug in and I see
> that it has generated an rspec encantation which I had not seen
> before:
>
>   it 'allows signup' do
> lambda do
>   create_user
>   response.should be_redirect
> end.should change(User, :count).by(1)
>   end
>
> What does the combination of a lambda and a .should mean?

This should answer your question:

http://rspec.info/rdoc/classes/Spec/Matchers.html#M000386

HTH,
David

>
> Similarly but a little different:
>
>   describe 'being created' do
> before do
>   @user = nil
>   @creating_user = lambda do
> @user = create_user
> violated "[EMAIL PROTECTED]" if
> @user.new_record?
>   end
> end
>
> it 'increments User#count' do
>   @creating_user.should change(User, :count).by(1)
> end
>   end
>
> Thanks for any pointers or clarifications!
> ___
> 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] Can you explain this code to me?

2008-02-07 Thread Bira
On Feb 7, 2008 9:26 PM, Pito Salas <[EMAIL PROTECTED]> wrote:

> What does the combination of a lambda and a .should mean?

I'm new to the list, but I think I can answer that :).

>From what I know, it means that RSpec will run the lambda's code and
check that it had the effect specified in the "should" call.

And from that I can determine by looking at the two incantations you
posted, the lambda is used because the code can throw an error, and
the spec's author didn't want the first error to prevent the outer
"should" from being checked.


-- 
Bira
http://compexplicita.wordpress.com
http://compexplicita.tumblr.com
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


[rspec-users] Can you explain this code to me?

2008-02-07 Thread Pito Salas
I am experimenting with the restful-authentication plug in and I see
that it has generated an rspec encantation which I had not seen
before:

  it 'allows signup' do
lambda do
  create_user
  response.should be_redirect
end.should change(User, :count).by(1)
  end

What does the combination of a lambda and a .should mean?

Similarly but a little different:

  describe 'being created' do
before do
  @user = nil
  @creating_user = lambda do
@user = create_user
violated "[EMAIL PROTECTED]" if
@user.new_record?
  end
end

it 'increments User#count' do
  @creating_user.should change(User, :count).by(1)
end
  end

Thanks for any pointers or clarifications!
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Problems testing rss feed controller.

2008-02-07 Thread Rick DeNatale
On 2/7/08, David Chelimsky <[EMAIL PROTECTED]> wrote:
> On Feb 7, 2008 2:27 PM, Rick DeNatale <[EMAIL PROTECTED]> wrote:
> > I've got a controller which produces an RSS feed, pretty much based on
> > Ryan Bates recent RailsCast.
> >
> > I'm having a heck of a time getting the spec to successfully fetch the
> > feed though.  I gave up got it working without a spec, and I'm now
> > trying to back up and get the spec to work.

>
> Try integrate_views:
>
> describe PodcastRssFeedController do
>   integrate_views
>   ...
> end
>
> Without that, render gets modified by rspec (to isolate your
> controllers from their views).

Thanks for the quick reply David, that was the problem, athough I
found that it needs to go into the nested example group, not the outer
one.

-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Problems testing rss feed controller.

2008-02-07 Thread David Chelimsky
On Feb 7, 2008 2:27 PM, Rick DeNatale <[EMAIL PROTECTED]> wrote:
> I've got a controller which produces an RSS feed, pretty much based on
> Ryan Bates recent RailsCast.
>
> I'm having a heck of a time getting the spec to successfully fetch the
> feed though.  I gave up got it working without a spec, and I'm now
> trying to back up and get the spec to work.
>
> The controller is simplicity itself:
>
> class PodcastRssFeedController < ApplicationController
>
>   def index
> @podcasts = Podcast.latest(100)
>   end
>
> end
>
> I have a route which gets to the index action and sets :format =>
> :rss,  as I said this produces a valid rss response in the real world,
> just not in the spec.
>
> Here's the basic spec:
>
> require File.dirname(__FILE__) + '/../spec_helper'
> require File.dirname(__FILE__) + '/../be_valid_feed'
>
> def mock_podcast(i)
>   mock_model(
>   Podcast,
>   :artist_name_slug => "Artist#{i}"
>   )
> end
>
> describe PodcastRssFeedController do
>
>   before(:all) do
> @podcasts = (1..5).map {|i| mock_podcast(i)}
> Podcast.stub!(:latest).and_return(@podcasts)
>   end
>
>   describe 'handling GET brightsidepodcasts/rss.xml' do
>
> def do_get
>   @request.format = 'rss'
>   get 'index', :format => :rss
> end
>
> it "should successfully get feed" do
>   do_get
>   response.should be_success
> end
>
> it "should validate" do
>   do_get
>   response.should be_valid_feed
> end
>   end
>
> end
>
> The validation example is failing because the response body is being returned 
> as
> podcast_rss_feed/index
>
> I've tried stepping through with rdebug and I can't figure out where
> it's going wrong.
>
> I eventually end up here:
>
>1099  logger.info("Rendering #{template_path}" + (status ?
> " (#{status})" : '')) if logger
> => 1100  render_for_text(@template.render_file(template_path,
> use_full_path, locals), status)
>1101end
>1102
>1103def render_for_text(text = nil, status = nil,
> append_response = false) #:nodoc:
>1104  @performed_render = true
> /Users/rick/Documents/terralien/brightside/site/vendor/rails/actionpack/lib/action_controller/base.rb:1100
>
> When I s(tep) into this I end up here:
>
>173  @template.metaclass.class_eval do
>174define_method :file_exists? do
>175  true
>176end
>177define_method :render_file do |*args|
> => 178  @first_render ||= args[0]
>179end
>180  end
>181end
>182  end
> /Users/rick/Documents/terralien/brightside/site/vendor/plugins/rspec_on_rails/lib/spec/rails/example/controller_example_group.rb:178
>
> It looks like the real ActionView::Base.render_file never gets called
> and that's where it looks like the realization that it's looking for
> rss rather than say html seems to happen.
>
> Can anyone shed any light on this?

Try integrate_views:

describe PodcastRssFeedController do
  integrate_views
  ...
end

Without that, render gets modified by rspec (to isolate your
controllers from their views).

HTH,
David


>
> --
> Rick DeNatale
>
> My blog on Ruby
> http://talklikeaduck.denhaven2.com/
> ___
> 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] Problems testing rss feed controller.

2008-02-07 Thread Rick DeNatale
I've got a controller which produces an RSS feed, pretty much based on
Ryan Bates recent RailsCast.

I'm having a heck of a time getting the spec to successfully fetch the
feed though.  I gave up got it working without a spec, and I'm now
trying to back up and get the spec to work.

The controller is simplicity itself:

class PodcastRssFeedController < ApplicationController

  def index
@podcasts = Podcast.latest(100)
  end

end

I have a route which gets to the index action and sets :format =>
:rss,  as I said this produces a valid rss response in the real world,
just not in the spec.

Here's the basic spec:

require File.dirname(__FILE__) + '/../spec_helper'
require File.dirname(__FILE__) + '/../be_valid_feed'

def mock_podcast(i)
  mock_model(
  Podcast,
  :artist_name_slug => "Artist#{i}"
  )
end

describe PodcastRssFeedController do

  before(:all) do
@podcasts = (1..5).map {|i| mock_podcast(i)}
Podcast.stub!(:latest).and_return(@podcasts)
  end

  describe 'handling GET brightsidepodcasts/rss.xml' do

def do_get
  @request.format = 'rss'
  get 'index', :format => :rss
end

it "should successfully get feed" do
  do_get
  response.should be_success
end

it "should validate" do
  do_get
  response.should be_valid_feed
end
  end

end

The validation example is failing because the response body is being returned as
podcast_rss_feed/index

I've tried stepping through with rdebug and I can't figure out where
it's going wrong.

I eventually end up here:

   1099  logger.info("Rendering #{template_path}" + (status ?
" (#{status})" : '')) if logger
=> 1100  render_for_text(@template.render_file(template_path,
use_full_path, locals), status)
   1101end
   1102
   1103def render_for_text(text = nil, status = nil,
append_response = false) #:nodoc:
   1104  @performed_render = true
/Users/rick/Documents/terralien/brightside/site/vendor/rails/actionpack/lib/action_controller/base.rb:1100

When I s(tep) into this I end up here:

   173  @template.metaclass.class_eval do
   174define_method :file_exists? do
   175  true
   176end
   177define_method :render_file do |*args|
=> 178  @first_render ||= args[0]
   179end
   180  end
   181end
   182  end
/Users/rick/Documents/terralien/brightside/site/vendor/plugins/rspec_on_rails/lib/spec/rails/example/controller_example_group.rb:178

It looks like the real ActionView::Base.render_file never gets called
and that's where it looks like the realization that it's looking for
rss rather than say html seems to happen.

Can anyone shed any light on this?

-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Shared examples not reloading in spec_server

2008-02-07 Thread David Chelimsky
On Feb 7, 2008 9:39 AM, Charles Grindel <[EMAIL PROTECTED]> wrote:
>
> I am currently running RSpec 1.1.3 with ZenTest 3.9.1 on Windows XP.  I have
> noticed that shared examples are not being reloaded by spec_server when they
> have been updated.  Below is a simple example that reproduces the problem.
> The example code and output is at the location below.
>
>
> http://pastie.caboo.se/148688
>
> I have confirmed that non-shared examples are being reloaded by spec_server.
> So, here are my questions.
>
> 1. Has anyone else noticed this behavior?
>
> 2. Does anyone know how I can force spec_server to reload the shared
> examples?
>
> 3. If this smells like a bug, can someone point me to instructions for
> filing a bug?

Sounds like a problem with your autotest configuration, not with
spec_server. Try it without spec server to verify that is correct.

If it's that autotest isn't loading them, you just need to add a
mapping in .autotest. Read the ZenTest docs for more info and/or check
out my blog post on this:
http://blog.davidchelimsky.net/articles/2008/01/15/rspec-1-1-2-and-zentest-3-8-0.

If you try that and you still think it's an rspec bug, file a ticket
at http://rspec.lighthouseapp.com.

Cheers,
David

>
> Thanks,
> Chuck
>
> ___
> 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] Shared examples not reloading in spec_server

2008-02-07 Thread Charles Grindel
I am currently running RSpec 1.1.3 with ZenTest 3.9.1 on Windows XP.  I have 
noticed that shared examples are not being reloaded by spec_server when they 
have been updated.  Below is a simple example that reproduces the problem.  The 
example code and output is at the location below.

http://pastie.caboo.se/148688

I have confirmed that non-shared examples are being reloaded by spec_server.  
So, here are my questions.

1. Has anyone else noticed this behavior?

2. Does anyone know how I can force spec_server to reload the shared examples?

3. If this smells like a bug, can someone point me to instructions for filing a 
bug?

Thanks,
Chuck___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] Rspec accessing the wrong id?

2008-02-07 Thread Jarkko Laine

On 6.2.2008, at 11.16, David Currin wrote:

> Hi,
>
> This is probably straightforward to solve. I'm new to rspec so I hope
> you'll be patient with me. I've created a model method which works in
> practice, but which rspec is not passing. I'm obviously not going
> about things in the right way in rspec so I'd appreciate any advice
> offered.
>
> Here's the (releveant parts of the) method which sets up a one to many
> relationship between Company and Projects.
>
> def.self create_project(record)
>parent_company = Company.find_by_basecampid(record.company.id)
>project = self.find_or_initialize_by_basecampid(record.id)
>project.company_id = parent_company.id
> end
>
> In rspec I've got this:
>
> def parent_company_attributes
>{
>  :id => 1,
>  :basecampid => 448718,
>  :name => "The Media Collective"
>}
>  end
>
> before do
>
> mock_record
> Company.should_receive(:find_by_basecampid).
>with(parent_company_attributes[:basecampid]).
>and_return(parent_company_attributes)

You're returning a hash here. If you call id for hash (like you do in  
this case), it will return the object id, not the element with key :id.

I think what you want to do is to create a mock_model with that hash  
and then stub Company.find_by_basecampid to return that mock object.

//jarkko

>
>
> @project = Project.create_project(record)
>
> end
>
> it "should populate company_id" do
>@project.company_id.should eql(parent_company_attributes[:id])
> end
>
>
> The problem is that it looks like rspec is using the object id rather
> than the id from parent_company_attributes. Here's the failure
> message:
>
> 1)
> 'Project.create project record should populate company_id' FAILED
> expected 1, got 8817400 (using .eql?)
>
> Thanks for any insights
>
> David Currin
> ___
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


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


Re: [rspec-users] outside-in BDD vs writing examples up front

2008-02-07 Thread Edvard Majakari
This is a short reply, but thinking about it a bit deeper, I have to
agree with Dan. What especially caught my
attention was this piece:

> If you start with a narrow, end-to-end piece of functionality and drill into
> it from the outside, right through until you have it working, you will find
> that at each layer of abstraction you will have a pretty focused subset of
> behaviour to implement to get the job done.

I think this comment is brilliant, as it is a more concrete viewpoint of
what "BDD manifesto" mentions about business value
(http://behaviour-driven.org/WheresTheBusinessValue).

As it happens, one of my main concerns with TDD was that it didn't
seem to have any strong opinions or rules about

  a) order of writing specifications regarding level of abstraction or
architectural structure (eg. I prefer V, C, M spec order in Rails, as
it starts from what the supposed client would see)

  b) how to pick the first test/spec you are going to implement.

Dan, I think your post gave an excellent answer (well, at least one)
to those questions -- and it also seems to be an opinion popular among
those few BDD practitioners _I know_ of; outside-in practically clears
out part a), as you are starting with high-level stuff like stories,
going deeper driven by requirements of higher level entities. Part b)
becomes much easier when you think about the clients priorities: what
feature gives the most business value for the client at the moment?
When starting an issue tracking system from the scratch, probably the
ability to submit new tickets becomes first -- that is, even before
being able to view them. Though quite obvious in retrospective, the
emphasis on business value in BDD gives IMO extra structure and
support for otherwise rather chaotic field of software engineering,
especially when the idea is applied in concrete rules of the
methodology.

Thanks!

-- 
"One day, when he was naughty, Mr Bunnsy looked over the hedge into
Farmer Fred's field and it was full of fresh green lettuces. Mr
Bunnsy, however, was not full of lettuces. This did not seem fair."
  -- Terry Pratchett, Mr. Bunnsy Has An Adventure
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users