Forum: RSpec Rspec approach to test model

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8b657063733482b2da943939535c4921?d=identicon&s=25 Kaleem Ullah (kaleemullah)
on 2009-02-11 14:51
Hi,

I am quite new to Rspec. I want to use Rspec to test my existing Code. I
start from Models (Unit Testing). Here i want your help on a issue.

Here is  model/user_spec.rb

describe User do
  before(:each) do
    @user=User.new
    @user.id='2'
    @user.email='kkkk@gmail.com'
    @user.password='1234'
    @user.crypted_password= '21066966a0578362e2115f3561bd4f144520ccd2'
    @user.salt= '574f09d3ae2473105567eab77ba9d3ae08ed40df'
    @user.remember_token= ''
    @user.remember_token_expires_at= ''
    @user.name= 'kaleem'
    @user.company_id='2'
    @user.title= ''
    @user.active= '1'
    @user.reset_password_token= ''
  end

  it "should authenticate with valid email,password and approved
company" do
    User.authenticate(@user.email, @user.password).should_not be_nil
  end

  it "should NOT authenticate with INvalid email" do
    @user.email=nil
    User.authenticate(@user.email, @user.password).should be_nil
  end

  it "should NOT authenticate with INvalid password" do
    @user.password=nil
    User.authenticate(@user.email, @user.password).should be_nil
  end

  it "should remember me" do
    @user.remember_me
    @user.remember_token.should_not be_nil
    @user.remember_token_expires_at.should_not be_nil
  end

  it "Remember Token time should be less than Token expires time" do
    @user.remember_token?.should be_true
  end

  it "should forget me" do
    @user.forget_me
    @user.remember_token.should be_nil
    @user.remember_token_expires_at.should be_nil
  end
end

Now Questions:
1) is my approach or way of writing specs is right???
2) How can i make specs for such a model action

   def activate!
    self.update_attribute(:active, true)
   end

Thank you :)
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick Hoffman (nickh)
on 2009-02-11 17:58
(Received via mailing list)
On 11/02/2009, at 8:51 AM, Kaleem Ullah wrote:
>    @user=User.new
>    @user.active= '1'
>    User.authenticate(@user.email, @user.password).should be_nil
>    @user.remember_token_expires_at.should_not be_nil
>  end
> Thank you :)
Hi Kaleem. All of that looks pretty good so far, though you shouldn't
be setting the "id" attribute of an AR model:
   @user.id = '2'

I suggest using the following format for creating your User object:

before(:each) do
   @user = User.new(
     :email => 'kkkk@gmail.com',
     :password => '1234',
     # ..etc..
   )
end

To spec your #activate! , why not do something like this?:

it 'should activate the user' do
   @user.active.should be_false
   @user.activate!
   @user.active.should be_true
end

Cheers,
Nick
369b9fd2a0d9e7cdbc60907a2a056ad9?d=identicon&s=25 Scott Taylor (Guest)
on 2009-02-11 18:01
(Received via mailing list)
On Feb 11, 2009, at 8:51 AM, Kaleem Ullah wrote:

>    @user=User.new
>    @user.active= '1'
>    @user.reset_password_token= ''
>  end

You could benefit from a factory / data builder.  See
FixtureReplacement, Fixjour, Factory girl, one of the many others out
there which would build this stuff *once* for you.

>  end
>  end
> end
>
> Now Questions:
> 1) is my approach or way of writing specs is right???

Remember: there is no right way of doing things.  Does it work for you
(and your team)? Is it clear?

>
> 2) How can i make specs for such a model action
>
>   def activate!
>    self.update_attribute(:active, true)
>   end

spec1:
@user.activate!
@user.should be_active

spec 2:
@user.should_not be_active


Scott
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-02-11 18:47
Scott Taylor wrote:
> On Feb 11, 2009, at 8:51 AM, Kaleem Ullah wrote:
>
>>    @user=User.new
>>    @user.active= '1'
>>    @user.reset_password_token= ''
>>  end
>
> You could benefit from a factory / data builder.  See
> FixtureReplacement, Fixjour, Factory girl, one of the many others out
> there which would build this stuff *once* for you.

Kaleem,

I recently installed nakajima-acts_as_fu (0.0.3) gem.  This provides a
rather painless way of specifying ActiveRecord model schemata on the fly
in your specifications.  You might find it helpful to look into this.

You will discover that some people favour mocks and stubs when dealing
with AR models in tests/specifications and some favour hitting the
database. Once you decide on your own preference in this matter the
choice of approach is somewhat simplified.
8b657063733482b2da943939535c4921?d=identicon&s=25 Kaleem Ullah (kaleemullah)
on 2009-02-11 18:50
Nick Hoffman wrote:
> To spec your #activate! , why not do something like this?:
>
> it 'should activate the user' do
>    @user.active.should be_false
>    @user.activate!
>    @user.active.should be_true
> end
>
>


Thanks for your reply Hoffman :)

I did the same but it gives error like "#23000Duplicate entry
'kkkkk@gmail.com Insert INTO (...) Values(...)".
It should do update not Insert.
But when i try call it with a new instance of user (with no values
assigned to any user attribute) then it works.
i.e.
it "should activate the user" do
 @user1=User.new
 @user1.activate!
end
This works.

I think i have to erase all the User attributes from "before (:each)"
and only assigned it in Examples which required it. am i right ?

Thanks
8b657063733482b2da943939535c4921?d=identicon&s=25 Kaleem Ullah (kaleemullah)
on 2009-02-11 18:57
James Byrne wrote:

> I recently installed nakajima-acts_as_fu (0.0.3) gem.  This provides a
> rather painless way of specifying ActiveRecord model schemata on the fly
> in your specifications.  You might find it helpful to look into this.
>
> You will discover that some people favour mocks and stubs when dealing
> with AR models in tests/specifications and some favour hitting the
> database. Once you decide on your own preference in this matter the
> choice of approach is somewhat simplified.

Hi james byrne,

Good to know that.I will surely look for this gem......

Thanks :)
8b657063733482b2da943939535c4921?d=identicon&s=25 Kaleem Ullah (kaleemullah)
on 2009-02-11 19:40
Scott Taylor wrote:
> On Feb 11, 2009, at 8:51 AM, Kaleem Ullah wrote:
>
>>    @user=User.new
>>    @user.active= '1'
>>    @user.reset_password_token= ''
>>  end
>
> You could benefit from a factory / data builder.  See
> FixtureReplacement, Fixjour, Factory girl, one of the many others out
> there which would build this stuff *once* for you.

Hi Scott Taylor,

I am in need of such data builders.

So nice of you... :)
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-02-12 08:15
(Received via mailing list)
Kaleem Ullah wrote:

> Scott Taylor wrote:

>> You could benefit from a factory / data builder.  See
>> FixtureReplacement, Fixjour, Factory girl, one of the many others out
>> there which would build this stuff *once* for you.

> I am in need of such data builders.

I recently thumped the square peg of FixtureDependencies into the round
hole of
Merb's RSpec.

   http://github.com/jeremyevans/fixture_dependencies...

I can answer questions about it if anyone wants to try it.

Except how to make it do rake db:fixtures:load. Does anyone know that
one?

--
   Phlip
8b657063733482b2da943939535c4921?d=identicon&s=25 Kaleem Ullah (kaleemullah)
on 2009-02-12 08:21
Hi all,
Below is my model action.

def activate!
    self.update_attribute(:active, true)
end

here is spec/models/user_spec.rb

describe User do
  before(:each) do
    @user=User.new(:email=>'kkk@gmail.com',:password='1234'....,)
  end

This is my spec.

it "should activate the user" do
  @user.activate!
  @user.active.should be_true
 end

 but it gives error like "#23000Duplicate entry
 'kkkkk@gmail.com Insert INTO (...) Values(...)".
 It should do update not Insert.
 But when i try call it with a new instance of user (with no values
 assigned to any user attribute) then it works.
 i.e.
 it "should activate the user" do
  @user_1=User.new
  @user_1.activate!
  @user_1.active.should be_true
 end

 I think i have to erase all the User attributes from "before (:each)"
 and only assigned it in Examples which required it.

 am i right ?

 Thanks
8a2de74362a8ee5f9c5e86b1ba0b1640?d=identicon&s=25 Joaquin Rivera Padron (Guest)
on 2009-02-12 11:35
(Received via mailing list)
kaleem,
method new does not run your model validations, neither does your
provided
activate! implementation.
but save indeed does.
my guess is your mixing several contexts (cases, situations) in your
single
spec. Try decoupling all with more granular examples and you'll get to
save
shore :-)
cheers,
joaquin
94ef1c99f4d4567563f576783979804f?d=identicon&s=25 Yury Kotlyarov (yura)
on 2009-02-12 13:57
(Received via mailing list)
Hi,

I am trying to write custom matcher which accepts block to spec views
with forms/fieldsets/inputs eg

view:
---
<form action='/users'>
  <fieldset>
    <legend>Personal Information</legend>
    <ol>
      <li>
        <label>First name</label>
        <input type="text" name="user[first_name]" />
      </li>
      ...
    </ol>
  </fieldset>
</form>

spec:
---

it "should have form with input fields" do
  render ...
  response.should have_form(users_path) do
    with_field_set 'Personal Information' do
      with_text_field 'First name', 'user[first_name]'
      ...
    end
  end
end

matches? of have_form:
---

    def matches?(response, &block)
      @block = block if block
      response.should @scope.have_tag('form[action=?]', @action) do
        @block.call.matches?(response)
      end
    end matches? of have_field_set: ---

    def matches?(response, &block)

      @block = block if block
      response.should @scope.with_tag('fieldset') do
        @scope.with_tag('legend', @legend) if @legend
        @scope.with_tag('ol') do
          @block.call.matches?(response)
        end
      end
    end but I get: NoMethodError in '/users/new should have form with
input fields' undefined method `matches?' for true:TrueClass
./spec/views/users/../../custom_ui_matchers/with_field_set.rb:10:in
`matches?'
./spec/views/users/../../custom_ui_matchers/have_form.rb:11:in
`matches?'
./spec/views/users/../../custom_ui_matchers/have_form.rb:10:in
`matches?' ./spec/views/users/new.html.haml_spec.rb:17:
/usr/lib64/ruby/1.8/timeout.rb:53:in `timeout' something wrong with
yielding block? Gist: http://gist.github.com/62562 Thanks in advance,
Yury
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-02-12 15:57
(Received via mailing list)
On Thu, Feb 12, 2009 at 5:52 AM, Yury Kotlyarov <yura@brainhouse.ru>
wrote:
>   <ol>
> ---
>
>   def matches?(response, &block)
> ./spec/views/users/../../custom_ui_matchers/with_field_set.rb:10:in
> `matches?' ./spec/views/users/../../custom_ui_matchers/have_form.rb:11:in
> `matches?' ./spec/views/users/../../custom_ui_matchers/have_form.rb:10:in
> `matches?' ./spec/views/users/new.html.haml_spec.rb:17:
> /usr/lib64/ruby/1.8/timeout.rb:53:in `timeout' something wrong with yielding
> block? Gist: http://gist.github.com/62562 Thanks in advance, Yury

What does initialize look like?
94ef1c99f4d4567563f576783979804f?d=identicon&s=25 Yury Kotlyarov (yura)
on 2009-02-12 18:14
(Received via mailing list)
David Chelimsky wrote:
>>  <fieldset>
>>
>>  end
>>   end matches? of have_field_set: ---
>>   end but I get: NoMethodError in '/users/new should have form with input
>
>

initialize for have_form:

class HaveForm     def initialize(action, scope, &block)
      @action, @scope, @block = action, scope, block
    end ... end

  def have_form(action, &block)
    HaveForm.new(action, self, &block)
  end


initialize for have_field_set:

  class WithFieldSet
    def initialize(scope, legend = nil, &block)
      @scope, @legend, @block = scope, legend, block
    end
  end

  def with_field_set(legend = nil, &block)
    WithFieldSet.new(self, legend, &block)
  end



btw I put it on gist: http://gist.github.com/62562
94ef1c99f4d4567563f576783979804f?d=identicon&s=25 Yury Kotlyarov (yura)
on 2009-02-13 00:11
(Received via mailing list)
SOLVED! just need to pass matched result to inner block. gist:
http://gist.github.com/62562

Many thanks,
Yury
94ef1c99f4d4567563f576783979804f?d=identicon&s=25 Yury Kotlyarov (yura)
on 2009-02-13 05:04
(Received via mailing list)
Yury Kotlyarov wrote:
> SOLVED! just need to pass matched result to inner block. gist:
> http://gist.github.com/62562
>
> Many thanks,
> Yury
>

I was wrong. It yields just one last line from the inner block. It
passes following:

  it "should have form to create a new user" do
    render '/users/new'
    response.should have_form(users_path) do
      with_field_set 'Personal Information' do
        with_text_field 'wrong', 'wrong' # there is no such field
with_text_field 'First Name', 'user[first_name]'
      end
    end
  end gist: http://gist.github.com/62562 TIA, Yury
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-02-13 21:01
(Received via mailing list)
Yury Kotlyarov wrote:

> it "should have form with input fields" do
>  render ...
>  response.should have_form(users_path) do
>    with_field_set 'Personal Information' do
>      with_text_field 'First name', 'user[first_name]'
>      ...
>    end
>  end
> end

The minor problem with that system is it forces your test to say exactly
what
the code says. That's not "driven" development!

If you can forbear to use matchers (shocked gasp!), at my day-job we
match
blocks all the time with assert2's new xpath system:

   require 'assert2/xpath'

   assert_xhtml response

   xpath :form, :action => users_path do
     xpath :fieldset, ?. => 'Personal Information' do
       xpath :input, :type => 'text', :name => 'user[first_name]' and
       xpath :input, :type => 'text', :name => 'user[last_name]'
     end
   end

 From there, wrapping the xpath() calls up into kewt with_text_field()
macros
would be trivial. They could also absolves the redundant 'user[]' text
on the
names, for example.

If any inner xpath() fails, there, the fault diagnostic contains a
formatted &
indented copy of the HTML block under inspection. The entire page would
not spew
out! Only the <form> or <fieldset> would.

--
   Phlip
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-02-13 21:17
(Received via mailing list)
On Fri, Feb 13, 2009 at 1:53 PM, Phlip <phlip2005@gmail.com> wrote:
>> end
>
>  xpath :form, :action => users_path do
>    xpath :fieldset, ?. => 'Personal Information' do
>      xpath :input, :type => 'text', :name => 'user[first_name]' and
>      xpath :input, :type => 'text', :name => 'user[last_name]'
>    end
>  end

Hey Philip,

This looks pretty cool. I wonder if you'd have any interest in making
this a bit more rspec-friendly? Something like an option to run it
like this:

expect_xpath do
 xpath :form, :action => users_path do
   xpath :fieldset, ?. => 'Personal Information' do
     xpath :input, :type => 'text', :name => 'user[first_name]' and
     xpath :input, :type => 'text', :name => 'user[last_name]'
   end
 end
end
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-02-14 01:06
(Received via mailing list)
David Chelimsky wrote:

> This looks pretty cool. I wonder if you'd have any interest in making
> this a bit more rspec-friendly? Something like an option to run it
> like this:
>
> expect_xpath do

It's on my do-list, but...

...are pluggable matchers as hard to write as the OP implied? How would
you fix
his problem? I need to know that before diving into the spaghetti that
Ruby
inevitably generates, just below every kewt DSL!
94ef1c99f4d4567563f576783979804f?d=identicon&s=25 Yury Kotlyarov (yura)
on 2009-02-14 22:34
(Received via mailing list)
Phlip wrote:
> ...are pluggable matchers as hard to write as the OP implied? How
> would you fix his problem? I need to know that before diving into the
> spaghetti that Ruby inevitably generates, just below every kewt DSL!
>

The major problem is fixed:
http://github.com/yura/howto-rspec-custom-matchers/

But there are still few open questions in README.rdoc

Regards,
Yury
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-02-14 22:41
(Received via mailing list)
Yury Kotlyarov wrote:

> The major problem is fixed:
> http://github.com/yura/howto-rspec-custom-matchers/

Apologies if I missed something, but is that a howto or a library? Do I
git it
and run it to ... learn to customize matchers?
94ef1c99f4d4567563f576783979804f?d=identicon&s=25 Yury Kotlyarov (yura)
on 2009-02-14 22:57
(Received via mailing list)
Phlip wrote:
> Yury Kotlyarov wrote:
>
>> The major problem is fixed:
>> http://github.com/yura/howto-rspec-custom-matchers/
>
> Apologies if I missed something, but is that a howto or a library? Do
> I git it and run it to ... learn to customize matchers?
>

RSpec is a library  ;-) And I have some issues with writing custom
matchers using rspec. Now I fixed my major showstopper but still have
open questions in the README.rdoc. Of course I hope this project will
help others to avoid issues I faced.
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-02-14 23:39
(Received via mailing list)
>>> http://github.com/yura/howto-rspec-custom-matchers/

> RSpec is a library  ;-)

Please try again: What is that source code drop. A website? A library?
or
something else?
94ef1c99f4d4567563f576783979804f?d=identicon&s=25 Yury Kotlyarov (yura)
on 2009-02-14 23:48
(Received via mailing list)
Phlip wrote:
>>>> http://github.com/yura/howto-rspec-custom-matchers/
>
>> RSpec is a library  ;-)
>
> Please try again: What is that source code drop. A website? A library?
> or something else?
>

ok - from README:

The project aims to encourage feedback on the best practices of creating
custom expectation matchers and specs for them.

Does it make sense?
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-02-15 01:15
(Received via mailing list)
Yury Kotlyarov wrote:

>>>>> http://github.com/yura/howto-rspec-custom-matchers/

> The project aims to encourage feedback on the best practices of creating
> custom expectation matchers and specs for them.
>
> Does it make sense?

That just says it's a "project".

One git clone later... It is a Rails 2.3.0 website!

TX; I will now go spelunking down inside it for a while. Wish me luck!
(-;
94ef1c99f4d4567563f576783979804f?d=identicon&s=25 Yury Kotlyarov (yura)
on 2009-02-15 08:06
(Received via mailing list)
Phlip wrote:
>
> One git clone later... It is a Rails 2.3.0 website!
>
> TX; I will now go spelunking down inside it for a while. Wish me luck!
> (-;

Yes, rails 2.3 app is just an environment to custom matchers
spec'ing/development. You can clone the git repo and just `rake spec` to
see my issues...

Those matchers (spec/lib/custom_ui_matchers/*) will be used in my real
project tho. But I still have minor issues which are listed in Open
questions section. Comments/remarks on the specs/code/etc are welcome!

Good luck!
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-02-16 05:14
(Received via mailing list)
Yury Kotlyarov wrote:
>> That just says it's a "project".
>>
>> One git clone later... It is a Rails 2.3.0 website!
>>
>> TX; I will now go spelunking down inside it for a while. Wish me luck!
>> (-;
>
> Yes, rails 2.3 app is just an environment to custom matchers
> spec'ing/development. You can clone the git repo and just `rake spec` to
> see my issues...

Special-needs programmer that I am, I declined to obtain the unreleased
2.3.0 of
Rails. I tweaked environment.rb back to 2.2.2.

That lead to a curious issue that an RSpec maintainer might understand:

----8<-----------------------------------------------------

$ rake spec

(in /home/phlip/projects/howto-rspec-custom-matchers)

/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require': no such file to load -- application
(MissingSourceFile)

         from
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
         from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:155:in
`require'
         from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:262:in
`require_or_load'
         from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:221:in
`depend_on'
         from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:133:in
`require_dependency'
         from
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:18:in
`define_dispatcher_callbacks'
         from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:182:in
`call'
         from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:182:in
`evaluate_method'
          ... 19 levels...
         from
/home/phlip/projects/howto-rspec-custom-matchers/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:14:in
`load_files'
         from
/home/phlip/projects/howto-rspec-custom-matchers/vendor/plugins/rspec/lib/spec/runner/options.rb:84:in
`run_examples'
         from
/home/phlip/projects/howto-rspec-custom-matchers/vendor/plugins/rspec/lib/spec/runner/command_line.rb:9:in
`run'
         from
/home/phlip/projects/howto-rspec-custom-matchers/vendor/plugins/rspec/bin/spec:4

----8<-----------------------------------------------------

Apologies for the ugly unformattable stack trace, but I don't know which
lines
are important.

I fixed the "missing application.rb" error via brute force: cp `find .
-name
application.rb`.

With a copy of application.rb in the root folder, 'rake spec' now works,
and I
can see some "Not Yet Implemented" issues...

> Those matchers (spec/lib/custom_ui_matchers/*) will be used in my real
> project tho. But I still have minor issues which are listed in Open
> questions section. Comments/remarks on the specs/code/etc are welcome!

Well, the rake spec generally passed, so someone else will have to tell
you
whatever it was you did wrong! (-:

--
   Phlip
   http://flea.sourceforge.net/PiglegToo_1.html
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-02-16 16:43
(Received via mailing list)
> This looks pretty cool. I wonder if you'd have any interest in making
> this a bit more rspec-friendly? Something like an option to run it
> like this:

Here's the spec. The sauce is below my signature.

   it 'should have a user form with the first name' do
     render '/users/new'
     response.body.should be_xml_with do
       xpath :form, :action => '/users' do
         xpath :fieldset do
           xpath :'legend[ contains(., "Personal Information") ]' and
           xpath :'label[ contains(., "First name") ]' and
           xpath :input, :type => 'text', :name => 'user[first_name]'
         end
       end
     end
   end

Now, two issues. Firstly, what is the point of writing verbiage,
designed for
review by the customer team, if they should not be expected to
understand
hardcore engineering gibberish beginning with "body.should be_xml_with
do"? A
sentence that is only partly English does more harm than good!

Secondly, when an xpath() fails, it prepares an elaborate and detailed
analysis
of the entire situation, packs this into a flunk(), and raises it in a
Test::Unit::AssertionFailedError.

Then RSpec then throws all that stuff away, and provides an incorrect
stack
trace to an internal error. Switching my 'user[first_name]' to
'user[first_nome]' provides this:

NoMethodError in '/users/new should have xpathic tags'
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.first
./spec/views/users/new.html.erb_spec.rb:50:

So if I were to rescue my own AssertionFailedError, and pack it into a
failure_message, wouldn't that effort be redundant?

--
   Phlip

require 'assert2/xpath'

Spec::Runner.configure do |c|
   c.include Test::Unit::Assertions
end  #  TODO blog this

   class BeXmlWith

     def initialize(scope, &block)
       @scope, @block = scope, block
     end

     def matches?(stwing, &block)
       waz_xdoc = @xdoc
       @block = block if block
       @scope.assert_xhtml stwing
       return (block || @block || proc{}).call
     ensure
       @xdoc = waz_xdoc
     end

     def failure_message
       "yack yack yack"
     end

     def negative_failure_message
       "yack yack yack"
     end
   end

   def be_xml_with(&block)
     BeXmlWith.new(self, &block)
   end
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-02-16 17:36
(Received via mailing list)
On Mon, Feb 16, 2009 at 9:36 AM, Phlip <phlip2005@gmail.com> wrote:
>        xpath :fieldset do
> understand hardcore engineering gibberish beginning with "body.should
> be_xml_with do"? A sentence that is only partly English does more harm than
> good!

When RSpec is used as customer facing, they see the docstrings
(strings passed to describe() and it()), not the internal code. That's
for developers.

> You might have expected an instance of Array.
> The error occurred while evaluating nil.first
> ./spec/views/users/new.html.erb_spec.rb:50:
>
> So if I were to rescue my own AssertionFailedError, and pack it into a
> failure_message, wouldn't that effort be redundant?

Take a look at
http://github.com/dchelimsky/rspec/blob/f6c75b1417...,
I think it'll solve this problem.

HTH,
David
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-02-17 03:27
(Received via mailing list)
David Chelimsky wrote:

> When RSpec is used as customer facing, they see the docstrings
> (strings passed to describe() and it()), not the internal code. That's
> for developers.

Then why the .should stuff? I'm a developer - technically - and I never
needed it!

But enough sophistry: Back to business...

> Take a look at 
http://github.com/dchelimsky/rspec/blob/f6c75b1417...,
> I think it'll solve this problem.

This gives the same issue:

   class BeXmlWith

     def matches?(stwing, &block)
       waz_xdoc = @xdoc

       @scope.wrap_expectation self do
         @scope.assert_xhtml stwing
         return (block || @block || proc{}).call
       end
     ensure
       @xdoc = waz_xdoc
     end

     attr_accessor :failure_message

The same error message for a stray nil:


1)
'/users/new should have xpathic tags' FAILED
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.first
./spec/views/users/new.html.erb_spec.rb:50:

line 50 is just this one:

     response.body.should be_xml_with do

and yes the response.body is populated...

--
   Phlip
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-02-17 03:44
(Received via mailing list)
> The same error message for a stray nil:

This attempt, calling simple_matcher directly, gives nearly the same
nil:

   def be_xml_with_(&block)
     waz_xdoc = @xdoc
     simple_matcher 'yo' do |given, matcher|
       wrap_expectation matcher do
         assert_xhtml given  #  this works
         block.call  #  crashes with a nil.first error!
       end
     end
   ensure
     @xdoc = waz_xdoc
   end

   it 'should have xpathic tags' do
     render '/users/new'

     response.body.should be_xml_with{  #  error points to this line
       xpath :form, :action => '/users'

So why no stack trace so I can diagnose this?
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-02-17 13:59
(Received via mailing list)
On Mon, Feb 16, 2009 at 8:21 PM, Phlip <phlip2005@gmail.com> wrote:
> David Chelimsky wrote:
>
>> When RSpec is used as customer facing, they see the docstrings
>> (strings passed to describe() and it()), not the internal code. That's
>> for developers.
>
> Then why the .should stuff? I'm a developer - technically - and I never
> needed it!

http://dannorth.net/introducing-bdd

>
>
> ./spec/views/users/new.html.erb_spec.rb:50:
>
> line 50 is just this one:
>
>    response.body.should be_xml_with do

That's the line you want to look at to know which example is failing.
To see the full backtrace, use the --backtrace (-b) option.

To learn about the various rspec options, use the --help (-h) option.

>
> and yes the response.body is populated...
This topic is locked and can not be replied to.