Re: [rspec-users] Rails - Mock going out of scope?

2007-07-18 Thread Mikel Lindsaar
Makes total sense.

Thank you _very_ much David :)  All works well and handles another
question I have on a disrelated matter.

By the way, loving RSpec.  Makes testing a lot more interesting and
rewarding.  Thanks for all your work.

Regards

Mikel

On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> D'oh. I should have figured this out on first sighting:
>
> The parent method does a find, so it retrieves a new object from the
> db - not the instance of Node you have defined. This new instance has
> no language associated with it (because the one you gave it pretends
> to exist by sporting an ID, but is never saved to the DB).
>
> Make sense?
>
> So, if you want to mock this, you have to mock a bit more than what
> you have in mind. Check out this pastie:
> http://pastie.textmate.org/80159
>
> David
>
> On 7/19/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > Using 1.2.3 and RSpec with the RSpec Rails plugin installed from
> > CURRENT on the RSpec SVN server.
> >
> > On 7/19/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > > Crap, I totally, forgot to mention, sorry David.  I am using 
> > > betternestedset
> > >
> > > http://wiki.rubyonrails.org/rails/pages/BetterNestedSet
> > >
> > > script/plugin source svn://rubyforge.org/var/svn/betternestedset
> > > script/plugin install betternestedset
> > >
> > >
> > > Regards
> > >
> > > Mikel
> > >
> > > On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> > > > Here are the errors I'm getting now:
> > > >
> > > > 1)
> > > > NoMethodError in 'Node instance should return it's parent's language
> > > > if it is a child'
> > > > undefined method `move_to_child_of' for #
> > > > ./spec/models/node_spec.rb:20:
> > > >
> > > > 2)
> > > > NameError in 'Node instance should return it's own language if it is 
> > > > root'
> > > > undefined local variable or method `parent' for #
> > > > /Users/david/projects/ruby/nodes/config/../app/models/node.rb:7:in
> > > > `language_name'
> > > > ./spec/models/node_spec.rb:16:
> > > >
> > > > What version of rails are you using? And is there a plugin you're
> > > > using for nested set?
> > > >
> > > > David
> > > >
> > > > On 7/19/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > > > > Here are the migrations:
> > > > >
> > > > > class CreateNodes < ActiveRecord::Migration
> > > > >   def self.up
> > > > > create_table ("nodes", :options => 'ENGINE=InnoDB DEFAULT
> > > > > CHARSET=utf8', :force => true) do |t|
> > > > >   t.column "title",:string
> > > > >   t.column "language_id",  :integer
> > > > >   t.column "parent_id",:integer
> > > > >   t.column "lft",  :integer
> > > > >   t.column "rgt",  :integer
> > > > >   t.column "original_node_id", :integer
> > > > >   t.column "owner_id", :integer
> > > > >   t.column "owner_type",   :string
> > > > >   t.column "root_id",  :integer
> > > > > end
> > > > >   end
> > > > >
> > > > >   def self.down
> > > > > drop_table "nodes"
> > > > >   end
> > > > > end
> > > > >
> > > > > class CreateLanguagesTable < ActiveRecord::Migration
> > > > >   def self.up
> > > > > create_table (:languages, :options => 'ENGINE=InnoDB DEFAULT
> > > > > CHARSET=utf8', :force => true) do |t|
> > > > >   t.column :name, :string
> > > > > end
> > > > >   end
> > > > >
> > > > >   def self.down
> > > > > drop_table :languages
> > > > >   end
> > > > > end
> > > > >
> > > > >
> > > > > The Nodes Model also has a self referrential association, but I don't
> > > > > think that would be causing any problems.
> > > > >
> > > > > class Node < ActiveRecord::Base
> > > > >   belongs_to :language
> > > > >   belongs_to :owner, :polymorphic => true
> > > > >
> > > > >   # Self referrential association, nodes have many original nodes - 
> > > > > keeps track
> > > > >   # of all original => translation associations
> > > > >   belongs_to :original_node, :class_name => "Node", :foreign_key =>
> > > > > "original_node_id"
> > > > >   has_many :translated_nodes, :class_name => "Node", :foreign_key =>
> > > > > "original_node_id"
> > > > >
> > > > >   acts_as_nested_set :scope => :root_id
> > > > >
> > > > >   #  + code from the original pastie
> > > > >
> > > > > end
> > > > >
> > > > >
> > > > > There are some other associations, but they are all belongs_to or
> > > > > has_many, so I haven't bothered to put all the tables in here and have
> > > > > removed the appropriate foreign keys from the nodes table.
> > > > >
> > > > >
> > > > > Regards
> > > > >
> > > > > Mikel
> > > > >
> > > > >
> > > > > On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> > > > > > Would you mind posting the migrations?
> > > > > >
> > > > > > On 7/18/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > > > > > > Heya David,
> > > > > > >
> > > > > > > Thanks for the reply.
> > > > > > >
> > > > > > > No, that didn't work, get the same error:
> > > > > > >
> > > > > > > NoMethodError in 'Node instance

Re: [rspec-users] Rails - Mock going out of scope?

2007-07-18 Thread David Chelimsky
D'oh. I should have figured this out on first sighting:

The parent method does a find, so it retrieves a new object from the
db - not the instance of Node you have defined. This new instance has
no language associated with it (because the one you gave it pretends
to exist by sporting an ID, but is never saved to the DB).

Make sense?

So, if you want to mock this, you have to mock a bit more than what
you have in mind. Check out this pastie:
http://pastie.textmate.org/80159

David

On 7/19/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> Using 1.2.3 and RSpec with the RSpec Rails plugin installed from
> CURRENT on the RSpec SVN server.
>
> On 7/19/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > Crap, I totally, forgot to mention, sorry David.  I am using betternestedset
> >
> > http://wiki.rubyonrails.org/rails/pages/BetterNestedSet
> >
> > script/plugin source svn://rubyforge.org/var/svn/betternestedset
> > script/plugin install betternestedset
> >
> >
> > Regards
> >
> > Mikel
> >
> > On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> > > Here are the errors I'm getting now:
> > >
> > > 1)
> > > NoMethodError in 'Node instance should return it's parent's language
> > > if it is a child'
> > > undefined method `move_to_child_of' for #
> > > ./spec/models/node_spec.rb:20:
> > >
> > > 2)
> > > NameError in 'Node instance should return it's own language if it is root'
> > > undefined local variable or method `parent' for #
> > > /Users/david/projects/ruby/nodes/config/../app/models/node.rb:7:in
> > > `language_name'
> > > ./spec/models/node_spec.rb:16:
> > >
> > > What version of rails are you using? And is there a plugin you're
> > > using for nested set?
> > >
> > > David
> > >
> > > On 7/19/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > > > Here are the migrations:
> > > >
> > > > class CreateNodes < ActiveRecord::Migration
> > > >   def self.up
> > > > create_table ("nodes", :options => 'ENGINE=InnoDB DEFAULT
> > > > CHARSET=utf8', :force => true) do |t|
> > > >   t.column "title",:string
> > > >   t.column "language_id",  :integer
> > > >   t.column "parent_id",:integer
> > > >   t.column "lft",  :integer
> > > >   t.column "rgt",  :integer
> > > >   t.column "original_node_id", :integer
> > > >   t.column "owner_id", :integer
> > > >   t.column "owner_type",   :string
> > > >   t.column "root_id",  :integer
> > > > end
> > > >   end
> > > >
> > > >   def self.down
> > > > drop_table "nodes"
> > > >   end
> > > > end
> > > >
> > > > class CreateLanguagesTable < ActiveRecord::Migration
> > > >   def self.up
> > > > create_table (:languages, :options => 'ENGINE=InnoDB DEFAULT
> > > > CHARSET=utf8', :force => true) do |t|
> > > >   t.column :name, :string
> > > > end
> > > >   end
> > > >
> > > >   def self.down
> > > > drop_table :languages
> > > >   end
> > > > end
> > > >
> > > >
> > > > The Nodes Model also has a self referrential association, but I don't
> > > > think that would be causing any problems.
> > > >
> > > > class Node < ActiveRecord::Base
> > > >   belongs_to :language
> > > >   belongs_to :owner, :polymorphic => true
> > > >
> > > >   # Self referrential association, nodes have many original nodes - 
> > > > keeps track
> > > >   # of all original => translation associations
> > > >   belongs_to :original_node, :class_name => "Node", :foreign_key =>
> > > > "original_node_id"
> > > >   has_many :translated_nodes, :class_name => "Node", :foreign_key =>
> > > > "original_node_id"
> > > >
> > > >   acts_as_nested_set :scope => :root_id
> > > >
> > > >   #  + code from the original pastie
> > > >
> > > > end
> > > >
> > > >
> > > > There are some other associations, but they are all belongs_to or
> > > > has_many, so I haven't bothered to put all the tables in here and have
> > > > removed the appropriate foreign keys from the nodes table.
> > > >
> > > >
> > > > Regards
> > > >
> > > > Mikel
> > > >
> > > >
> > > > On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> > > > > Would you mind posting the migrations?
> > > > >
> > > > > On 7/18/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > > > > > Heya David,
> > > > > >
> > > > > > Thanks for the reply.
> > > > > >
> > > > > > No, that didn't work, get the same error:
> > > > > >
> > > > > > NoMethodError in 'Node instance should return it's parent's language
> > > > > > if it is a child'
> > > > > > You have a nil object when you didn't expect it!
> > > > > > The error occurred while evaluating nil.name
> > > > > >
> > > > > >
> > > > > > If I include the fixture :languages, then replace out @language =
> > > > > > mock_model... with
> > > > > >
> > > > > > @language = languages(:one)
> > > > > >
> > > > > > it all works dandy.  But I'm trying to ween myself off fixtures :)
> > > > > >
> > > > > > Regards
> > > > > >
> > > > > > Mikel
> > > > > >
> > > > > >
> > > > > >
> > > > > > On 7/19/07, D

Re: [rspec-users] need help getting a word right

2007-07-18 Thread Mikel Lindsaar
I like the it "should..." do end calls, specifiy as well makes sense.

So what is the problem?  I think we should take a look at what comments are for.

Essentially the "should blah" text acts as a comment on the spec.  One
that is then picked up by RSpec and inserted to make our
specifications more readable in one line.

So, if we treat these "shoulds..." as essentially comments, it then
opens the door to a resolution as the only place you should use
comments is when the code itself is not immediately self documenting.

In the Spec world, this really means anything that is more than say
one or two lines of code.

Why?  Well, that's just an opinion.  But we could make the following
best practice:

If your spec utilizes one matcher and fits on one line, then use:

specify { target.should == blah }

if your spec can not be succinctly and clearly described in one line
of code, then you should add a spec "comment" and so use

it "should accept an XML feed" do
 xml = mock(XMLFeed)
 target.handle_feed(xml).should == true
end

The point of specs is their readability and clarity.  By making that
simple distinction as a "best practice" you handle the situation.

That way reading down the spec code would be almost as clear as
looking at the RSpec doc html page.

then again, with text editors typing "it" and writing a spec is
really not THAT hard is it?

Regards

Mikel

On 7/19/07, Robert Feldt <[EMAIL PROTECTED]> wrote:
>
>
> On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> > Hey all,
> >
> > I see examples showing up that look like this:
> >
> > describe Thing do
> >   before(:each) do
> > @thing = Thing.new
> >   end
> >
> >   it do
> > @thing.should be_something
> >   end
> > end
> >
> > This will produce output like this:
> >
> > Thing
> > - should be something
> >
> > But "it do" is driving me mad :(
> >
> > We need a better word. Of course, 'specify' has not been completely
> > removed, so you can still do this:
> >
> > describe Thing do
> >   before(:each) { @thing = Thing.new }
> >   specify { @thing.should be_something }
> > end
> >
> > Consise? Yes. But I'm not psyched about 'specify' either. There IS a
> > perfect word for this situation. What is it? Suggestions?
>
> I like "should" or "spec" the best.
>
> While I'm at it why not "given" instead of "describe"?
>
> Cheers,
>
> Robert Feldt
>
>
> ___
> 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] need help getting a word right

2007-07-18 Thread barsalou
Well in that specific case, please seems like a good choice

   please do
  @thing.should be_somthing
   end

But If you had text in between then it wouldn't seem the same:

   please "whatever text" do
  @thing.should be_something
   end



Quoting David Chelimsky <[EMAIL PROTECTED]>:

> Hey all,
>
> I see examples showing up that look like this:
>
> describe Thing do
>  before(:each) do
>@thing = Thing.new
>  end
>
>  it do
>@thing.should be_something
>  end
> end
>
> This will produce output like this:
>
> Thing
> - should be something
>
> But "it do" is driving me mad :(
>
> We need a better word. Of course, 'specify' has not been completely
> removed, so you can still do this:
>
> describe Thing do
>  before(:each) { @thing = Thing.new }
>  specify { @thing.should be_something }
> end
>
> Consise? Yes. But I'm not psyched about 'specify' either. There IS a
> perfect word for this situation. What is it? Suggestions?
>
> Thanks,
> David
> ___
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>




This message was sent using IMP, the Internet Messaging Program.

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


Re: [rspec-users] Rails - Mock going out of scope?

2007-07-18 Thread Mikel Lindsaar
Using 1.2.3 and RSpec with the RSpec Rails plugin installed from
CURRENT on the RSpec SVN server.

On 7/19/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> Crap, I totally, forgot to mention, sorry David.  I am using betternestedset
>
> http://wiki.rubyonrails.org/rails/pages/BetterNestedSet
>
> script/plugin source svn://rubyforge.org/var/svn/betternestedset
> script/plugin install betternestedset
>
>
> Regards
>
> Mikel
>
> On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> > Here are the errors I'm getting now:
> >
> > 1)
> > NoMethodError in 'Node instance should return it's parent's language
> > if it is a child'
> > undefined method `move_to_child_of' for #
> > ./spec/models/node_spec.rb:20:
> >
> > 2)
> > NameError in 'Node instance should return it's own language if it is root'
> > undefined local variable or method `parent' for #
> > /Users/david/projects/ruby/nodes/config/../app/models/node.rb:7:in
> > `language_name'
> > ./spec/models/node_spec.rb:16:
> >
> > What version of rails are you using? And is there a plugin you're
> > using for nested set?
> >
> > David
> >
> > On 7/19/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > > Here are the migrations:
> > >
> > > class CreateNodes < ActiveRecord::Migration
> > >   def self.up
> > > create_table ("nodes", :options => 'ENGINE=InnoDB DEFAULT
> > > CHARSET=utf8', :force => true) do |t|
> > >   t.column "title",:string
> > >   t.column "language_id",  :integer
> > >   t.column "parent_id",:integer
> > >   t.column "lft",  :integer
> > >   t.column "rgt",  :integer
> > >   t.column "original_node_id", :integer
> > >   t.column "owner_id", :integer
> > >   t.column "owner_type",   :string
> > >   t.column "root_id",  :integer
> > > end
> > >   end
> > >
> > >   def self.down
> > > drop_table "nodes"
> > >   end
> > > end
> > >
> > > class CreateLanguagesTable < ActiveRecord::Migration
> > >   def self.up
> > > create_table (:languages, :options => 'ENGINE=InnoDB DEFAULT
> > > CHARSET=utf8', :force => true) do |t|
> > >   t.column :name, :string
> > > end
> > >   end
> > >
> > >   def self.down
> > > drop_table :languages
> > >   end
> > > end
> > >
> > >
> > > The Nodes Model also has a self referrential association, but I don't
> > > think that would be causing any problems.
> > >
> > > class Node < ActiveRecord::Base
> > >   belongs_to :language
> > >   belongs_to :owner, :polymorphic => true
> > >
> > >   # Self referrential association, nodes have many original nodes - keeps 
> > > track
> > >   # of all original => translation associations
> > >   belongs_to :original_node, :class_name => "Node", :foreign_key =>
> > > "original_node_id"
> > >   has_many :translated_nodes, :class_name => "Node", :foreign_key =>
> > > "original_node_id"
> > >
> > >   acts_as_nested_set :scope => :root_id
> > >
> > >   #  + code from the original pastie
> > >
> > > end
> > >
> > >
> > > There are some other associations, but they are all belongs_to or
> > > has_many, so I haven't bothered to put all the tables in here and have
> > > removed the appropriate foreign keys from the nodes table.
> > >
> > >
> > > Regards
> > >
> > > Mikel
> > >
> > >
> > > On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> > > > Would you mind posting the migrations?
> > > >
> > > > On 7/18/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > > > > Heya David,
> > > > >
> > > > > Thanks for the reply.
> > > > >
> > > > > No, that didn't work, get the same error:
> > > > >
> > > > > NoMethodError in 'Node instance should return it's parent's language
> > > > > if it is a child'
> > > > > You have a nil object when you didn't expect it!
> > > > > The error occurred while evaluating nil.name
> > > > >
> > > > >
> > > > > If I include the fixture :languages, then replace out @language =
> > > > > mock_model... with
> > > > >
> > > > > @language = languages(:one)
> > > > >
> > > > > it all works dandy.  But I'm trying to ween myself off fixtures :)
> > > > >
> > > > > Regards
> > > > >
> > > > > Mikel
> > > > >
> > > > >
> > > > >
> > > > > On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> > > > > > On 7/18/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > > > > > > Hello list,
> > > > > > >
> > > > > > > I think I have a rails related RSpec problem with a mock going 
> > > > > > > out of
> > > > > > > scope on a recursive call to a model.
> > > > > > >
> > > > > > > The code is at: http://pastie.textmate.org/79821 if you want to 
> > > > > > > see it
> > > > > > > highlighted.  I have pasted it below as well.
> > > > > > >
> > > > > > > Basically, I have an acts_as_nested_set model called "Node", which
> > > > > > > works fine.  I have a function which finds the language name of 
> > > > > > > the
> > > > > > > node instance.  If the language is nil for the node instance being
> > > > > > > queried, it then recursively calles language_name on it

Re: [rspec-users] Rails - Mock going out of scope?

2007-07-18 Thread Mikel Lindsaar
Crap, I totally, forgot to mention, sorry David.  I am using betternestedset

http://wiki.rubyonrails.org/rails/pages/BetterNestedSet

script/plugin source svn://rubyforge.org/var/svn/betternestedset
script/plugin install betternestedset


Regards

Mikel

On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> Here are the errors I'm getting now:
>
> 1)
> NoMethodError in 'Node instance should return it's parent's language
> if it is a child'
> undefined method `move_to_child_of' for #
> ./spec/models/node_spec.rb:20:
>
> 2)
> NameError in 'Node instance should return it's own language if it is root'
> undefined local variable or method `parent' for #
> /Users/david/projects/ruby/nodes/config/../app/models/node.rb:7:in
> `language_name'
> ./spec/models/node_spec.rb:16:
>
> What version of rails are you using? And is there a plugin you're
> using for nested set?
>
> David
>
> On 7/19/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > Here are the migrations:
> >
> > class CreateNodes < ActiveRecord::Migration
> >   def self.up
> > create_table ("nodes", :options => 'ENGINE=InnoDB DEFAULT
> > CHARSET=utf8', :force => true) do |t|
> >   t.column "title",:string
> >   t.column "language_id",  :integer
> >   t.column "parent_id",:integer
> >   t.column "lft",  :integer
> >   t.column "rgt",  :integer
> >   t.column "original_node_id", :integer
> >   t.column "owner_id", :integer
> >   t.column "owner_type",   :string
> >   t.column "root_id",  :integer
> > end
> >   end
> >
> >   def self.down
> > drop_table "nodes"
> >   end
> > end
> >
> > class CreateLanguagesTable < ActiveRecord::Migration
> >   def self.up
> > create_table (:languages, :options => 'ENGINE=InnoDB DEFAULT
> > CHARSET=utf8', :force => true) do |t|
> >   t.column :name, :string
> > end
> >   end
> >
> >   def self.down
> > drop_table :languages
> >   end
> > end
> >
> >
> > The Nodes Model also has a self referrential association, but I don't
> > think that would be causing any problems.
> >
> > class Node < ActiveRecord::Base
> >   belongs_to :language
> >   belongs_to :owner, :polymorphic => true
> >
> >   # Self referrential association, nodes have many original nodes - keeps 
> > track
> >   # of all original => translation associations
> >   belongs_to :original_node, :class_name => "Node", :foreign_key =>
> > "original_node_id"
> >   has_many :translated_nodes, :class_name => "Node", :foreign_key =>
> > "original_node_id"
> >
> >   acts_as_nested_set :scope => :root_id
> >
> >   #  + code from the original pastie
> >
> > end
> >
> >
> > There are some other associations, but they are all belongs_to or
> > has_many, so I haven't bothered to put all the tables in here and have
> > removed the appropriate foreign keys from the nodes table.
> >
> >
> > Regards
> >
> > Mikel
> >
> >
> > On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> > > Would you mind posting the migrations?
> > >
> > > On 7/18/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > > > Heya David,
> > > >
> > > > Thanks for the reply.
> > > >
> > > > No, that didn't work, get the same error:
> > > >
> > > > NoMethodError in 'Node instance should return it's parent's language
> > > > if it is a child'
> > > > You have a nil object when you didn't expect it!
> > > > The error occurred while evaluating nil.name
> > > >
> > > >
> > > > If I include the fixture :languages, then replace out @language =
> > > > mock_model... with
> > > >
> > > > @language = languages(:one)
> > > >
> > > > it all works dandy.  But I'm trying to ween myself off fixtures :)
> > > >
> > > > Regards
> > > >
> > > > Mikel
> > > >
> > > >
> > > >
> > > > On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> > > > > On 7/18/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > > > > > Hello list,
> > > > > >
> > > > > > I think I have a rails related RSpec problem with a mock going out 
> > > > > > of
> > > > > > scope on a recursive call to a model.
> > > > > >
> > > > > > The code is at: http://pastie.textmate.org/79821 if you want to see 
> > > > > > it
> > > > > > highlighted.  I have pasted it below as well.
> > > > > >
> > > > > > Basically, I have an acts_as_nested_set model called "Node", which
> > > > > > works fine.  I have a function which finds the language name of the
> > > > > > node instance.  If the language is nil for the node instance being
> > > > > > queried, it then recursively calles language_name on it's parent 
> > > > > > until
> > > > > > one of them has the language.  Then this gets returned.
> > > > > >
> > > > > > When I do this with a fixture, it works fine.  Ie, a Database call 
> > > > > > can
> > > > > > be made to a language table and I get the language name.
> > > > > >
> > > > > > In the code attached it has a langauge instance being mocked.  I get
> > > > > > the same result if I mock Language.should_receive(:find)...
> > > > > >
> > > >

Re: [rspec-users] Rails - Mock going out of scope?

2007-07-18 Thread David Chelimsky
Here are the errors I'm getting now:

1)
NoMethodError in 'Node instance should return it's parent's language
if it is a child'
undefined method `move_to_child_of' for #
./spec/models/node_spec.rb:20:

2)
NameError in 'Node instance should return it's own language if it is root'
undefined local variable or method `parent' for #
/Users/david/projects/ruby/nodes/config/../app/models/node.rb:7:in
`language_name'
./spec/models/node_spec.rb:16:

What version of rails are you using? And is there a plugin you're
using for nested set?

David

On 7/19/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> Here are the migrations:
>
> class CreateNodes < ActiveRecord::Migration
>   def self.up
> create_table ("nodes", :options => 'ENGINE=InnoDB DEFAULT
> CHARSET=utf8', :force => true) do |t|
>   t.column "title",:string
>   t.column "language_id",  :integer
>   t.column "parent_id",:integer
>   t.column "lft",  :integer
>   t.column "rgt",  :integer
>   t.column "original_node_id", :integer
>   t.column "owner_id", :integer
>   t.column "owner_type",   :string
>   t.column "root_id",  :integer
> end
>   end
>
>   def self.down
> drop_table "nodes"
>   end
> end
>
> class CreateLanguagesTable < ActiveRecord::Migration
>   def self.up
> create_table (:languages, :options => 'ENGINE=InnoDB DEFAULT
> CHARSET=utf8', :force => true) do |t|
>   t.column :name, :string
> end
>   end
>
>   def self.down
> drop_table :languages
>   end
> end
>
>
> The Nodes Model also has a self referrential association, but I don't
> think that would be causing any problems.
>
> class Node < ActiveRecord::Base
>   belongs_to :language
>   belongs_to :owner, :polymorphic => true
>
>   # Self referrential association, nodes have many original nodes - keeps 
> track
>   # of all original => translation associations
>   belongs_to :original_node, :class_name => "Node", :foreign_key =>
> "original_node_id"
>   has_many :translated_nodes, :class_name => "Node", :foreign_key =>
> "original_node_id"
>
>   acts_as_nested_set :scope => :root_id
>
>   #  + code from the original pastie
>
> end
>
>
> There are some other associations, but they are all belongs_to or
> has_many, so I haven't bothered to put all the tables in here and have
> removed the appropriate foreign keys from the nodes table.
>
>
> Regards
>
> Mikel
>
>
> On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> > Would you mind posting the migrations?
> >
> > On 7/18/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > > Heya David,
> > >
> > > Thanks for the reply.
> > >
> > > No, that didn't work, get the same error:
> > >
> > > NoMethodError in 'Node instance should return it's parent's language
> > > if it is a child'
> > > You have a nil object when you didn't expect it!
> > > The error occurred while evaluating nil.name
> > >
> > >
> > > If I include the fixture :languages, then replace out @language =
> > > mock_model... with
> > >
> > > @language = languages(:one)
> > >
> > > it all works dandy.  But I'm trying to ween myself off fixtures :)
> > >
> > > Regards
> > >
> > > Mikel
> > >
> > >
> > >
> > > On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> > > > On 7/18/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > > > > Hello list,
> > > > >
> > > > > I think I have a rails related RSpec problem with a mock going out of
> > > > > scope on a recursive call to a model.
> > > > >
> > > > > The code is at: http://pastie.textmate.org/79821 if you want to see it
> > > > > highlighted.  I have pasted it below as well.
> > > > >
> > > > > Basically, I have an acts_as_nested_set model called "Node", which
> > > > > works fine.  I have a function which finds the language name of the
> > > > > node instance.  If the language is nil for the node instance being
> > > > > queried, it then recursively calles language_name on it's parent until
> > > > > one of them has the language.  Then this gets returned.
> > > > >
> > > > > When I do this with a fixture, it works fine.  Ie, a Database call can
> > > > > be made to a language table and I get the language name.
> > > > >
> > > > > In the code attached it has a langauge instance being mocked.  I get
> > > > > the same result if I mock Language.should_receive(:find)...
> > > > >
> > > > > It SEEMS like the Mock is going out of scope on the recursive call to
> > > > > parent.  The direct spec to the parent to get language name works
> > > > > fine.
> > > > >
> > > > > Any ideas? (the code below is slimmed down to the code needed to run 
> > > > > the spec.
> > > > >
> > > > > Regards
> > > > >
> > > > > Mikel
> > > > >
> > > > > CODE::
> > > > >
> > > > > class Node < ActiveRecord::Base
> > > > >
> > > > >   belongs_to :language
> > > > >   acts_as_nested_set :scope => :root_id
> > > > >
> > > > >   def language_name
> > > > > self.root? ? language.name : parent.language_name
> > > > >   end
> > > > > end
> > >

Re: [rspec-users] Rails - Mock going out of scope?

2007-07-18 Thread Mikel Lindsaar
Here are the migrations:

class CreateNodes < ActiveRecord::Migration
  def self.up
create_table ("nodes", :options => 'ENGINE=InnoDB DEFAULT
CHARSET=utf8', :force => true) do |t|
  t.column "title",:string
  t.column "language_id",  :integer
  t.column "parent_id",:integer
  t.column "lft",  :integer
  t.column "rgt",  :integer
  t.column "original_node_id", :integer
  t.column "owner_id", :integer
  t.column "owner_type",   :string
  t.column "root_id",  :integer
end
  end

  def self.down
drop_table "nodes"
  end
end

class CreateLanguagesTable < ActiveRecord::Migration
  def self.up
create_table (:languages, :options => 'ENGINE=InnoDB DEFAULT
CHARSET=utf8', :force => true) do |t|
  t.column :name, :string
end
  end

  def self.down
drop_table :languages
  end
end


The Nodes Model also has a self referrential association, but I don't
think that would be causing any problems.

class Node < ActiveRecord::Base
  belongs_to :language
  belongs_to :owner, :polymorphic => true

  # Self referrential association, nodes have many original nodes - keeps track
  # of all original => translation associations
  belongs_to :original_node, :class_name => "Node", :foreign_key =>
"original_node_id"
  has_many :translated_nodes, :class_name => "Node", :foreign_key =>
"original_node_id"

  acts_as_nested_set :scope => :root_id

  #  + code from the original pastie

end


There are some other associations, but they are all belongs_to or
has_many, so I haven't bothered to put all the tables in here and have
removed the appropriate foreign keys from the nodes table.


Regards

Mikel


On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> Would you mind posting the migrations?
>
> On 7/18/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > Heya David,
> >
> > Thanks for the reply.
> >
> > No, that didn't work, get the same error:
> >
> > NoMethodError in 'Node instance should return it's parent's language
> > if it is a child'
> > You have a nil object when you didn't expect it!
> > The error occurred while evaluating nil.name
> >
> >
> > If I include the fixture :languages, then replace out @language =
> > mock_model... with
> >
> > @language = languages(:one)
> >
> > it all works dandy.  But I'm trying to ween myself off fixtures :)
> >
> > Regards
> >
> > Mikel
> >
> >
> >
> > On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> > > On 7/18/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > > > Hello list,
> > > >
> > > > I think I have a rails related RSpec problem with a mock going out of
> > > > scope on a recursive call to a model.
> > > >
> > > > The code is at: http://pastie.textmate.org/79821 if you want to see it
> > > > highlighted.  I have pasted it below as well.
> > > >
> > > > Basically, I have an acts_as_nested_set model called "Node", which
> > > > works fine.  I have a function which finds the language name of the
> > > > node instance.  If the language is nil for the node instance being
> > > > queried, it then recursively calles language_name on it's parent until
> > > > one of them has the language.  Then this gets returned.
> > > >
> > > > When I do this with a fixture, it works fine.  Ie, a Database call can
> > > > be made to a language table and I get the language name.
> > > >
> > > > In the code attached it has a langauge instance being mocked.  I get
> > > > the same result if I mock Language.should_receive(:find)...
> > > >
> > > > It SEEMS like the Mock is going out of scope on the recursive call to
> > > > parent.  The direct spec to the parent to get language name works
> > > > fine.
> > > >
> > > > Any ideas? (the code below is slimmed down to the code needed to run 
> > > > the spec.
> > > >
> > > > Regards
> > > >
> > > > Mikel
> > > >
> > > > CODE::
> > > >
> > > > class Node < ActiveRecord::Base
> > > >
> > > >   belongs_to :language
> > > >   acts_as_nested_set :scope => :root_id
> > > >
> > > >   def language_name
> > > > self.root? ? language.name : parent.language_name
> > > >   end
> > > > end
> > > >
> > > > describe Node, "instance" do
> > > >
> > > >   fixtures :nodes
> > > >
> > > >   before(:each) do
> > > > @language = mock_model(Language, :name => "Japanese")
> > > > @node = Node.create!(:language => @language)
> > > > @section1 = Node.create!()
> > > > @chapter1 = Node.create!()
> > > >   end
> > > >
> > > >   it "should return it's own language if it is root" do  # Passes
> > > > 
> > > > @language.should_receive(:name).exactly(:once).and_return("Japanese")
> > > > @node.language_name.should == "Japanese"
> > > >   end
> > > >
> > > >   it "should return it's parent's language if it is a child" do #
> > > > Fails (message below)
> > > > @section1.move_to_child_of(@node)
> > > > @chapter1.move_to_child_of(@section1)
> > > > 
> > > > @language.should_receive(:name).exactly(:once).and_return("Japanese")

Re: [rspec-users] Rails - Mock going out of scope?

2007-07-18 Thread David Chelimsky
You might also want to try this w/ another mock framework like mocha
or flexmock (or rr if you're on edge). If you do, please report the
results here.

Thx

On 7/18/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> Would you mind posting the migrations?
>
> On 7/18/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > Heya David,
> >
> > Thanks for the reply.
> >
> > No, that didn't work, get the same error:
> >
> > NoMethodError in 'Node instance should return it's parent's language
> > if it is a child'
> > You have a nil object when you didn't expect it!
> > The error occurred while evaluating nil.name
> >
> >
> > If I include the fixture :languages, then replace out @language =
> > mock_model... with
> >
> > @language = languages(:one)
> >
> > it all works dandy.  But I'm trying to ween myself off fixtures :)
> >
> > Regards
> >
> > Mikel
> >
> >
> >
> > On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> > > On 7/18/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > > > Hello list,
> > > >
> > > > I think I have a rails related RSpec problem with a mock going out of
> > > > scope on a recursive call to a model.
> > > >
> > > > The code is at: http://pastie.textmate.org/79821 if you want to see it
> > > > highlighted.  I have pasted it below as well.
> > > >
> > > > Basically, I have an acts_as_nested_set model called "Node", which
> > > > works fine.  I have a function which finds the language name of the
> > > > node instance.  If the language is nil for the node instance being
> > > > queried, it then recursively calles language_name on it's parent until
> > > > one of them has the language.  Then this gets returned.
> > > >
> > > > When I do this with a fixture, it works fine.  Ie, a Database call can
> > > > be made to a language table and I get the language name.
> > > >
> > > > In the code attached it has a langauge instance being mocked.  I get
> > > > the same result if I mock Language.should_receive(:find)...
> > > >
> > > > It SEEMS like the Mock is going out of scope on the recursive call to
> > > > parent.  The direct spec to the parent to get language name works
> > > > fine.
> > > >
> > > > Any ideas? (the code below is slimmed down to the code needed to run 
> > > > the spec.
> > > >
> > > > Regards
> > > >
> > > > Mikel
> > > >
> > > > CODE::
> > > >
> > > > class Node < ActiveRecord::Base
> > > >
> > > >   belongs_to :language
> > > >   acts_as_nested_set :scope => :root_id
> > > >
> > > >   def language_name
> > > > self.root? ? language.name : parent.language_name
> > > >   end
> > > > end
> > > >
> > > > describe Node, "instance" do
> > > >
> > > >   fixtures :nodes
> > > >
> > > >   before(:each) do
> > > > @language = mock_model(Language, :name => "Japanese")
> > > > @node = Node.create!(:language => @language)
> > > > @section1 = Node.create!()
> > > > @chapter1 = Node.create!()
> > > >   end
> > > >
> > > >   it "should return it's own language if it is root" do  # Passes
> > > > 
> > > > @language.should_receive(:name).exactly(:once).and_return("Japanese")
> > > > @node.language_name.should == "Japanese"
> > > >   end
> > > >
> > > >   it "should return it's parent's language if it is a child" do #
> > > > Fails (message below)
> > > > @section1.move_to_child_of(@node)
> > > > @chapter1.move_to_child_of(@section1)
> > > > 
> > > > @language.should_receive(:name).exactly(:once).and_return("Japanese")
> > > > @section1.language_name.should == "Japanese"
> > > > 
> > > > @language.should_receive(:name).exactly(:once).and_return("Japanese")
> > > > @chapter1.language_name.should == "Japanese"
> > > >   end
> > > > end
> > >
> > > It's generally not recommended that you set expectations, invoke them
> > > and then set them again. I'm not sure, but that may be the problem
> > > here. Try this:
> > >
> > >   it "should return it's parent's language if it is a child" do #
> > > Fails (message below)
> > > @section1.move_to_child_of(@node)
> > > @chapter1.move_to_child_of(@section1)
> > > @language.should_receive(:name).exactly(:twice).and_return("Japanese")
> > > @section1.language_name.should == "Japanese"
> > > @chapter1.language_name.should == "Japanese"
> > >   end
> > >
> > > Does that work?
> > >
> > > >
> > > > SPEC ERROR::
> > > >
> > > > NoMethodError in 'Node instance should return it's parent's language
> > > > if it is a child'
> > > > You have a nil object when you didn't expect it!
> > > > The error occurred while evaluating nil.name
> > > > /Users/mikel/working/universal_translator/config/../app/models/node.rb:29:in
> > > > 'language_name'
> > > > /Users/mikel/working/universal_translator/config/../app/models/node.rb:29:in
> > > > 'language_name'
> > > > ./spec/models/node_spec.rb:160:
> > > > script/spec:4:
> > > > ___
> > > > rspec-users mailing list
> > > > rspec-users@rubyforge.org
> > > > http://rubyforge.org/mailman/listinfo/rspec-users
> > > >
> > > 

Re: [rspec-users] Rails - Mock going out of scope?

2007-07-18 Thread David Chelimsky
Would you mind posting the migrations?

On 7/18/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> Heya David,
>
> Thanks for the reply.
>
> No, that didn't work, get the same error:
>
> NoMethodError in 'Node instance should return it's parent's language
> if it is a child'
> You have a nil object when you didn't expect it!
> The error occurred while evaluating nil.name
>
>
> If I include the fixture :languages, then replace out @language =
> mock_model... with
>
> @language = languages(:one)
>
> it all works dandy.  But I'm trying to ween myself off fixtures :)
>
> Regards
>
> Mikel
>
>
>
> On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> > On 7/18/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > > Hello list,
> > >
> > > I think I have a rails related RSpec problem with a mock going out of
> > > scope on a recursive call to a model.
> > >
> > > The code is at: http://pastie.textmate.org/79821 if you want to see it
> > > highlighted.  I have pasted it below as well.
> > >
> > > Basically, I have an acts_as_nested_set model called "Node", which
> > > works fine.  I have a function which finds the language name of the
> > > node instance.  If the language is nil for the node instance being
> > > queried, it then recursively calles language_name on it's parent until
> > > one of them has the language.  Then this gets returned.
> > >
> > > When I do this with a fixture, it works fine.  Ie, a Database call can
> > > be made to a language table and I get the language name.
> > >
> > > In the code attached it has a langauge instance being mocked.  I get
> > > the same result if I mock Language.should_receive(:find)...
> > >
> > > It SEEMS like the Mock is going out of scope on the recursive call to
> > > parent.  The direct spec to the parent to get language name works
> > > fine.
> > >
> > > Any ideas? (the code below is slimmed down to the code needed to run the 
> > > spec.
> > >
> > > Regards
> > >
> > > Mikel
> > >
> > > CODE::
> > >
> > > class Node < ActiveRecord::Base
> > >
> > >   belongs_to :language
> > >   acts_as_nested_set :scope => :root_id
> > >
> > >   def language_name
> > > self.root? ? language.name : parent.language_name
> > >   end
> > > end
> > >
> > > describe Node, "instance" do
> > >
> > >   fixtures :nodes
> > >
> > >   before(:each) do
> > > @language = mock_model(Language, :name => "Japanese")
> > > @node = Node.create!(:language => @language)
> > > @section1 = Node.create!()
> > > @chapter1 = Node.create!()
> > >   end
> > >
> > >   it "should return it's own language if it is root" do  # Passes
> > > @language.should_receive(:name).exactly(:once).and_return("Japanese")
> > > @node.language_name.should == "Japanese"
> > >   end
> > >
> > >   it "should return it's parent's language if it is a child" do #
> > > Fails (message below)
> > > @section1.move_to_child_of(@node)
> > > @chapter1.move_to_child_of(@section1)
> > > @language.should_receive(:name).exactly(:once).and_return("Japanese")
> > > @section1.language_name.should == "Japanese"
> > > @language.should_receive(:name).exactly(:once).and_return("Japanese")
> > > @chapter1.language_name.should == "Japanese"
> > >   end
> > > end
> >
> > It's generally not recommended that you set expectations, invoke them
> > and then set them again. I'm not sure, but that may be the problem
> > here. Try this:
> >
> >   it "should return it's parent's language if it is a child" do #
> > Fails (message below)
> > @section1.move_to_child_of(@node)
> > @chapter1.move_to_child_of(@section1)
> > @language.should_receive(:name).exactly(:twice).and_return("Japanese")
> > @section1.language_name.should == "Japanese"
> > @chapter1.language_name.should == "Japanese"
> >   end
> >
> > Does that work?
> >
> > >
> > > SPEC ERROR::
> > >
> > > NoMethodError in 'Node instance should return it's parent's language
> > > if it is a child'
> > > You have a nil object when you didn't expect it!
> > > The error occurred while evaluating nil.name
> > > /Users/mikel/working/universal_translator/config/../app/models/node.rb:29:in
> > > 'language_name'
> > > /Users/mikel/working/universal_translator/config/../app/models/node.rb:29:in
> > > 'language_name'
> > > ./spec/models/node_spec.rb:160:
> > > script/spec:4:
> > > ___
> > > 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 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] Rails - Mock going out of scope?

2007-07-18 Thread Mikel Lindsaar
Heya David,

Thanks for the reply.

No, that didn't work, get the same error:

NoMethodError in 'Node instance should return it's parent's language
if it is a child'
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.name


If I include the fixture :languages, then replace out @language =
mock_model... with

@language = languages(:one)

it all works dandy.  But I'm trying to ween myself off fixtures :)

Regards

Mikel



On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:
> On 7/18/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> > Hello list,
> >
> > I think I have a rails related RSpec problem with a mock going out of
> > scope on a recursive call to a model.
> >
> > The code is at: http://pastie.textmate.org/79821 if you want to see it
> > highlighted.  I have pasted it below as well.
> >
> > Basically, I have an acts_as_nested_set model called "Node", which
> > works fine.  I have a function which finds the language name of the
> > node instance.  If the language is nil for the node instance being
> > queried, it then recursively calles language_name on it's parent until
> > one of them has the language.  Then this gets returned.
> >
> > When I do this with a fixture, it works fine.  Ie, a Database call can
> > be made to a language table and I get the language name.
> >
> > In the code attached it has a langauge instance being mocked.  I get
> > the same result if I mock Language.should_receive(:find)...
> >
> > It SEEMS like the Mock is going out of scope on the recursive call to
> > parent.  The direct spec to the parent to get language name works
> > fine.
> >
> > Any ideas? (the code below is slimmed down to the code needed to run the 
> > spec.
> >
> > Regards
> >
> > Mikel
> >
> > CODE::
> >
> > class Node < ActiveRecord::Base
> >
> >   belongs_to :language
> >   acts_as_nested_set :scope => :root_id
> >
> >   def language_name
> > self.root? ? language.name : parent.language_name
> >   end
> > end
> >
> > describe Node, "instance" do
> >
> >   fixtures :nodes
> >
> >   before(:each) do
> > @language = mock_model(Language, :name => "Japanese")
> > @node = Node.create!(:language => @language)
> > @section1 = Node.create!()
> > @chapter1 = Node.create!()
> >   end
> >
> >   it "should return it's own language if it is root" do  # Passes
> > @language.should_receive(:name).exactly(:once).and_return("Japanese")
> > @node.language_name.should == "Japanese"
> >   end
> >
> >   it "should return it's parent's language if it is a child" do #
> > Fails (message below)
> > @section1.move_to_child_of(@node)
> > @chapter1.move_to_child_of(@section1)
> > @language.should_receive(:name).exactly(:once).and_return("Japanese")
> > @section1.language_name.should == "Japanese"
> > @language.should_receive(:name).exactly(:once).and_return("Japanese")
> > @chapter1.language_name.should == "Japanese"
> >   end
> > end
>
> It's generally not recommended that you set expectations, invoke them
> and then set them again. I'm not sure, but that may be the problem
> here. Try this:
>
>   it "should return it's parent's language if it is a child" do #
> Fails (message below)
> @section1.move_to_child_of(@node)
> @chapter1.move_to_child_of(@section1)
> @language.should_receive(:name).exactly(:twice).and_return("Japanese")
> @section1.language_name.should == "Japanese"
> @chapter1.language_name.should == "Japanese"
>   end
>
> Does that work?
>
> >
> > SPEC ERROR::
> >
> > NoMethodError in 'Node instance should return it's parent's language
> > if it is a child'
> > You have a nil object when you didn't expect it!
> > The error occurred while evaluating nil.name
> > /Users/mikel/working/universal_translator/config/../app/models/node.rb:29:in
> > 'language_name'
> > /Users/mikel/working/universal_translator/config/../app/models/node.rb:29:in
> > 'language_name'
> > ./spec/models/node_spec.rb:160:
> > script/spec:4:
> > ___
> > 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 mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] need help getting a word right

2007-07-18 Thread Robert Feldt

On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:


Hey all,

I see examples showing up that look like this:

describe Thing do
  before(:each) do
@thing = Thing.new
  end

  it do
@thing.should be_something
  end
end

This will produce output like this:

Thing
- should be something

But "it do" is driving me mad :(

We need a better word. Of course, 'specify' has not been completely
removed, so you can still do this:

describe Thing do
  before(:each) { @thing = Thing.new }
  specify { @thing.should be_something }
end

Consise? Yes. But I'm not psyched about 'specify' either. There IS a
perfect word for this situation. What is it? Suggestions?



I like "should" or "spec" the best.

While I'm at it why not "given" instead of "describe"?

Cheers,

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

Re: [rspec-users] need help getting a word right

2007-07-18 Thread Kyle Hargraves
David Chelimsky wrote:
> Consise? Yes. But I'm not psyched about 'specify' either. There IS a
> perfect word for this situation. What is it? Suggestions?

Personally, I disagree. For examples like that, I think 'specify' is a 
pretty ideal word. It reads exactly as I want it to. Depending on what 
I'm describing, I tend to use either 'it' or 'specify' exclusively, but 
I definitely do mix them throughout my projects. (I always use 
'describe,' though.)

The only problem I ever have is when the specify block needs to be 
larger than a line, and 'specify do' reads poorly -- which may be what 
you have against it, too. So far, I've just tried to avoid that...

Of course, I say that now, and then someone will have a great suggestion 
for a word that makes tons more sense.

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


Re: [rspec-users] need help getting a word right

2007-07-18 Thread Daniel N

On 7/19/07, David Chelimsky <[EMAIL PROTECTED]> wrote:


Hey all,

I see examples showing up that look like this:

describe Thing do
  before(:each) do
@thing = Thing.new
  end

  it do
@thing.should be_something
  end
end

This will produce output like this:

Thing
- should be something

But "it do" is driving me mad :(

We need a better word. Of course, 'specify' has not been completely
removed, so you can still do this:

describe Thing do
  before(:each) { @thing = Thing.new }
  specify { @thing.should be_something }
end

Consise? Yes. But I'm not psyched about 'specify' either. There IS a
perfect word for this situation. What is it? Suggestions?

Thanks,
David



What about conform

describe Thing do
 before(:each) { @thing = Thing.new }
 conform { @thing.should be_something }
end
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

[rspec-users] need help getting a word right

2007-07-18 Thread David Chelimsky
Hey all,

I see examples showing up that look like this:

describe Thing do
  before(:each) do
@thing = Thing.new
  end

  it do
@thing.should be_something
  end
end

This will produce output like this:

Thing
- should be something

But "it do" is driving me mad :(

We need a better word. Of course, 'specify' has not been completely
removed, so you can still do this:

describe Thing do
  before(:each) { @thing = Thing.new }
  specify { @thing.should be_something }
end

Consise? Yes. But I'm not psyched about 'specify' either. There IS a
perfect word for this situation. What is it? Suggestions?

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


Re: [rspec-users] Rails - Mock going out of scope?

2007-07-18 Thread David Chelimsky
On 7/18/07, Mikel Lindsaar <[EMAIL PROTECTED]> wrote:
> Hello list,
>
> I think I have a rails related RSpec problem with a mock going out of
> scope on a recursive call to a model.
>
> The code is at: http://pastie.textmate.org/79821 if you want to see it
> highlighted.  I have pasted it below as well.
>
> Basically, I have an acts_as_nested_set model called "Node", which
> works fine.  I have a function which finds the language name of the
> node instance.  If the language is nil for the node instance being
> queried, it then recursively calles language_name on it's parent until
> one of them has the language.  Then this gets returned.
>
> When I do this with a fixture, it works fine.  Ie, a Database call can
> be made to a language table and I get the language name.
>
> In the code attached it has a langauge instance being mocked.  I get
> the same result if I mock Language.should_receive(:find)...
>
> It SEEMS like the Mock is going out of scope on the recursive call to
> parent.  The direct spec to the parent to get language name works
> fine.
>
> Any ideas? (the code below is slimmed down to the code needed to run the spec.
>
> Regards
>
> Mikel
>
> CODE::
>
> class Node < ActiveRecord::Base
>
>   belongs_to :language
>   acts_as_nested_set :scope => :root_id
>
>   def language_name
> self.root? ? language.name : parent.language_name
>   end
> end
>
> describe Node, "instance" do
>
>   fixtures :nodes
>
>   before(:each) do
> @language = mock_model(Language, :name => "Japanese")
> @node = Node.create!(:language => @language)
> @section1 = Node.create!()
> @chapter1 = Node.create!()
>   end
>
>   it "should return it's own language if it is root" do  # Passes
> @language.should_receive(:name).exactly(:once).and_return("Japanese")
> @node.language_name.should == "Japanese"
>   end
>
>   it "should return it's parent's language if it is a child" do #
> Fails (message below)
> @section1.move_to_child_of(@node)
> @chapter1.move_to_child_of(@section1)
> @language.should_receive(:name).exactly(:once).and_return("Japanese")
> @section1.language_name.should == "Japanese"
> @language.should_receive(:name).exactly(:once).and_return("Japanese")
> @chapter1.language_name.should == "Japanese"
>   end
> end

It's generally not recommended that you set expectations, invoke them
and then set them again. I'm not sure, but that may be the problem
here. Try this:

  it "should return it's parent's language if it is a child" do #
Fails (message below)
@section1.move_to_child_of(@node)
@chapter1.move_to_child_of(@section1)
@language.should_receive(:name).exactly(:twice).and_return("Japanese")
@section1.language_name.should == "Japanese"
@chapter1.language_name.should == "Japanese"
  end

Does that work?

>
> SPEC ERROR::
>
> NoMethodError in 'Node instance should return it's parent's language
> if it is a child'
> You have a nil object when you didn't expect it!
> The error occurred while evaluating nil.name
> /Users/mikel/working/universal_translator/config/../app/models/node.rb:29:in
> 'language_name'
> /Users/mikel/working/universal_translator/config/../app/models/node.rb:29:in
> 'language_name'
> ./spec/models/node_spec.rb:160:
> script/spec:4:
> ___
> 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] stopping on first failure

2007-07-18 Thread David Chelimsky
On 7/18/07, Esad Hajdarevic <[EMAIL PROTECTED]> wrote:
> Hi!
>
> Is there an option that I could provide to the spec runner that would
> make it stop after it
> encounters the first spec that fails?

Not yet. Please submit an RFE if you'd like to see it:
http://rubyforge.org/tracker/?group_id=797

>
> Esad
>
> ___
> 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] stopping on first failure

2007-07-18 Thread Esad Hajdarevic
Hi!

Is there an option that I could provide to the spec runner that would 
make it stop after it
encounters the first spec that fails?

Esad

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


Re: [rspec-users] Mocking Rails association collections

2007-07-18 Thread David Chelimsky
On 7/18/07, court3nay <[EMAIL PROTECTED]> wrote:
> Any chance of some prettier syntax for that?

There's always a chance. What do you propose?

>
> ---
> Courtenay
>
> On Jul 18, 2007, at 2:30 PM, "David Chelimsky" <[EMAIL PROTECTED]>
> wrote:
>
> > On 7/18/07, Paul <[EMAIL PROTECTED]> wrote:
> >> Rails model association collections allow you to do nifty things
> >> like:
> >>
> >>  article.comments.find(:all, :conditions => {:created_at >
> >> 1.day.ago})
> >>
> >> Has anyone found a good way to mock this up? I'm currently doing
> >> this:
> >>
> >>  @comment1 = mock_model(Comment)
> >>  comments = mock(Array)
> >>  comments.stub!(:find).and_return([EMAIL PROTECTED])
> >>
> >>  @article = mock_model(Article)
> >>  @article.stub!(:comments).and_return(comments)
> >>
> >> I don't like this, because of that intermediate 'comments' object,
> >> whose
> >> only purpose is so that i can stub the chained method. I'd like to do
> >> something like this:
> >>
> >>  @comment1 = mock_model(Comment)
> >>
> >>  @article = mock_model(Article, :comments => mock(Array, :find =>
> >> [EMAIL PROTECTED]))
> >>
> >> But trying this causes an error: "Mock 'Array' received unexpected
> >> message :find with (:all, ...)" because you can't inline stubs with
> >> ordinary `mock`. I can replace it with `mock_model`, but this feels
> >> unclean.
> >>
> >> Has anyone come across a good 'best-practice' solution to this
> >> problem?
> >
> > You can use the stub() method instead of mock() to inline method
> > stubs:
> >
> > @article = mock_model(
> >  Article, :comments => stub(Array, :find => [EMAIL PROTECTED])
> > )
> >
> > The mock() method works differently because it does different stuff w/
> > the Hash under the covers.
> >
> >>
> >> TIA,
> >> Paul Sadauskas
> >>
> >> ___
> >> 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 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] Rails - Mock going out of scope?

2007-07-18 Thread Mikel Lindsaar
Hello list,

I think I have a rails related RSpec problem with a mock going out of
scope on a recursive call to a model.

The code is at: http://pastie.textmate.org/79821 if you want to see it
highlighted.  I have pasted it below as well.

Basically, I have an acts_as_nested_set model called "Node", which
works fine.  I have a function which finds the language name of the
node instance.  If the language is nil for the node instance being
queried, it then recursively calles language_name on it's parent until
one of them has the language.  Then this gets returned.

When I do this with a fixture, it works fine.  Ie, a Database call can
be made to a language table and I get the language name.

In the code attached it has a langauge instance being mocked.  I get
the same result if I mock Language.should_receive(:find)...

It SEEMS like the Mock is going out of scope on the recursive call to
parent.  The direct spec to the parent to get language name works
fine.

Any ideas? (the code below is slimmed down to the code needed to run the spec.

Regards

Mikel

CODE::

class Node < ActiveRecord::Base

  belongs_to :language
  acts_as_nested_set :scope => :root_id

  def language_name
self.root? ? language.name : parent.language_name
  end
end

describe Node, "instance" do

  fixtures :nodes

  before(:each) do
@language = mock_model(Language, :name => "Japanese")
@node = Node.create!(:language => @language)
@section1 = Node.create!()
@chapter1 = Node.create!()
  end

  it "should return it's own language if it is root" do  # Passes
@language.should_receive(:name).exactly(:once).and_return("Japanese")
@node.language_name.should == "Japanese"
  end

  it "should return it's parent's language if it is a child" do #
Fails (message below)
@section1.move_to_child_of(@node)
@chapter1.move_to_child_of(@section1)
@language.should_receive(:name).exactly(:once).and_return("Japanese")
@section1.language_name.should == "Japanese"
@language.should_receive(:name).exactly(:once).and_return("Japanese")
@chapter1.language_name.should == "Japanese"
  end
end

SPEC ERROR::

NoMethodError in 'Node instance should return it's parent's language
if it is a child'
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.name
/Users/mikel/working/universal_translator/config/../app/models/node.rb:29:in
'language_name'
/Users/mikel/working/universal_translator/config/../app/models/node.rb:29:in
'language_name'
./spec/models/node_spec.rb:160:
script/spec:4:
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Mocking Rails association collections

2007-07-18 Thread Paul
I eventually ended up using this, from a somewhat-related post to this 
list a couple months ago:

def assoc_mock(stubs = {})
  proxy = mock('association_proxy')
  stubs.each do |method, ret|
proxy.stub!(method).and_return(ret)
  end
  proxy
end

Then I can do something like I wanted:

@comment = mock_model(Comment)

@article = mock_model(Article, :comments => (@comment = assoc_mock(:find 
=> [EMAIL PROTECTED])))

But this works just like the stub!(Foo, :method => return). I might keep 
mine, though, its a little more explicit as to what the 'in-between' 
collection is.

Thanks,
Paul
 


David Chelimsky wrote:
> On 7/18/07, Paul <[EMAIL PROTECTED]> wrote:
>   
>> Rails model association collections allow you to do nifty things like:
>>
>>   article.comments.find(:all, :conditions => {:created_at > 1.day.ago})
>>
>> Has anyone found a good way to mock this up? I'm currently doing this:
>>
>>   @comment1 = mock_model(Comment)
>>   comments = mock(Array)
>>   comments.stub!(:find).and_return([EMAIL PROTECTED])
>>
>>   @article = mock_model(Article)
>>   @article.stub!(:comments).and_return(comments)
>>
>> I don't like this, because of that intermediate 'comments' object, whose
>> only purpose is so that i can stub the chained method. I'd like to do
>> something like this:
>>
>>   @comment1 = mock_model(Comment)
>>
>>   @article = mock_model(Article, :comments => mock(Array, :find =>
>> [EMAIL PROTECTED]))
>>
>> But trying this causes an error: "Mock 'Array' received unexpected
>> message :find with (:all, ...)" because you can't inline stubs with
>> ordinary `mock`. I can replace it with `mock_model`, but this feels unclean.
>>
>> Has anyone come across a good 'best-practice' solution to this problem?
>> 
>
> You can use the stub() method instead of mock() to inline method stubs:
>
> @article = mock_model(
>   Article, :comments => stub(Array, :find => [EMAIL PROTECTED])
> )
>
> The mock() method works differently because it does different stuff w/
> the Hash under the covers.
>
>   
>> TIA,
>> Paul Sadauskas
>>
>> ___
>> 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 mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Mocking Rails association collections

2007-07-18 Thread court3nay
Any chance of some prettier syntax for that?

---
Courtenay

On Jul 18, 2007, at 2:30 PM, "David Chelimsky" <[EMAIL PROTECTED]>  
wrote:

> On 7/18/07, Paul <[EMAIL PROTECTED]> wrote:
>> Rails model association collections allow you to do nifty things  
>> like:
>>
>>  article.comments.find(:all, :conditions => {:created_at >  
>> 1.day.ago})
>>
>> Has anyone found a good way to mock this up? I'm currently doing  
>> this:
>>
>>  @comment1 = mock_model(Comment)
>>  comments = mock(Array)
>>  comments.stub!(:find).and_return([EMAIL PROTECTED])
>>
>>  @article = mock_model(Article)
>>  @article.stub!(:comments).and_return(comments)
>>
>> I don't like this, because of that intermediate 'comments' object,  
>> whose
>> only purpose is so that i can stub the chained method. I'd like to do
>> something like this:
>>
>>  @comment1 = mock_model(Comment)
>>
>>  @article = mock_model(Article, :comments => mock(Array, :find =>
>> [EMAIL PROTECTED]))
>>
>> But trying this causes an error: "Mock 'Array' received unexpected
>> message :find with (:all, ...)" because you can't inline stubs with
>> ordinary `mock`. I can replace it with `mock_model`, but this feels  
>> unclean.
>>
>> Has anyone come across a good 'best-practice' solution to this  
>> problem?
>
> You can use the stub() method instead of mock() to inline method  
> stubs:
>
> @article = mock_model(
>  Article, :comments => stub(Array, :find => [EMAIL PROTECTED])
> )
>
> The mock() method works differently because it does different stuff w/
> the Hash under the covers.
>
>>
>> TIA,
>> Paul Sadauskas
>>
>> ___
>> 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 mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Mocking Rails association collections

2007-07-18 Thread court3nay
Any chance of some prettier syntax for that?

---
Courtenay

On Jul 18, 2007, at 2:30 PM, "David Chelimsky" <[EMAIL PROTECTED]>  
wrote:

> On 7/18/07, Paul <[EMAIL PROTECTED]> wrote:
>> Rails model association collections allow you to do nifty things  
>> like:
>>
>> article.comments.find(:all, :conditions => {:created_at > 1.day.ago})
>>
>> Has anyone found a good way to mock this up? I'm currently doing  
>> this:
>>
>> @comment1 = mock_model(Comment)
>> comments = mock(Array)
>> comments.stub!(:find).and_return([EMAIL PROTECTED])
>>
>> @article = mock_model(Article)
>> @article.stub!(:comments).and_return(comments)
>>
>> I don't like this, because of that intermediate 'comments' object,  
>> whose
>> only purpose is so that i can stub the chained method. I'd like to do
>> something like this:
>>
>> @comment1 = mock_model(Comment)
>>
>> @article = mock_model(Article, :comments => mock(Array, :find =>
>> [EMAIL PROTECTED]))
>>
>> But trying this causes an error: "Mock 'Array' received unexpected
>> message :find with (:all, ...)" because you can't inline stubs with
>> ordinary `mock`. I can replace it with `mock_model`, but this feels  
>> unclean.
>>
>> Has anyone come across a good 'best-practice' solution to this  
>> problem?
>
> You can use the stub() method instead of mock() to inline method  
> stubs:
>
> @article = mock_model(
> Article, :comments => stub(Array, :find => [EMAIL PROTECTED])
> )
>
> The mock() method works differently because it does different stuff w/
> the Hash under the covers.
>
>>
>> TIA,
>> Paul Sadauskas
>>
>> ___
>> 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 mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Mocking Rails association collections

2007-07-18 Thread David Chelimsky
On 7/18/07, Paul <[EMAIL PROTECTED]> wrote:
> Rails model association collections allow you to do nifty things like:
>
>   article.comments.find(:all, :conditions => {:created_at > 1.day.ago})
>
> Has anyone found a good way to mock this up? I'm currently doing this:
>
>   @comment1 = mock_model(Comment)
>   comments = mock(Array)
>   comments.stub!(:find).and_return([EMAIL PROTECTED])
>
>   @article = mock_model(Article)
>   @article.stub!(:comments).and_return(comments)
>
> I don't like this, because of that intermediate 'comments' object, whose
> only purpose is so that i can stub the chained method. I'd like to do
> something like this:
>
>   @comment1 = mock_model(Comment)
>
>   @article = mock_model(Article, :comments => mock(Array, :find =>
> [EMAIL PROTECTED]))
>
> But trying this causes an error: "Mock 'Array' received unexpected
> message :find with (:all, ...)" because you can't inline stubs with
> ordinary `mock`. I can replace it with `mock_model`, but this feels unclean.
>
> Has anyone come across a good 'best-practice' solution to this problem?

You can use the stub() method instead of mock() to inline method stubs:

@article = mock_model(
  Article, :comments => stub(Array, :find => [EMAIL PROTECTED])
)

The mock() method works differently because it does different stuff w/
the Hash under the covers.

>
> TIA,
> Paul Sadauskas
>
> ___
> 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] Mocking Rails association collections

2007-07-18 Thread Paul
Rails model association collections allow you to do nifty things like:

  article.comments.find(:all, :conditions => {:created_at > 1.day.ago})

Has anyone found a good way to mock this up? I'm currently doing this:

  @comment1 = mock_model(Comment)
  comments = mock(Array)
  comments.stub!(:find).and_return([EMAIL PROTECTED])

  @article = mock_model(Article)
  @article.stub!(:comments).and_return(comments)

I don't like this, because of that intermediate 'comments' object, whose 
only purpose is so that i can stub the chained method. I'd like to do 
something like this:

  @comment1 = mock_model(Comment)

  @article = mock_model(Article, :comments => mock(Array, :find => 
[EMAIL PROTECTED]))

But trying this causes an error: "Mock 'Array' received unexpected 
message :find with (:all, ...)" because you can't inline stubs with 
ordinary `mock`. I can replace it with `mock_model`, but this feels unclean.

Has anyone come across a good 'best-practice' solution to this problem?

TIA,
Paul Sadauskas

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