Rspec approach to test model

Phlip wrote:

Yury K. 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 :wink: 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.

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

RSpec is a library :wink:

Please try again: What is that source code drop. A website? A library?
or
something else?

Phlip wrote:

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

RSpec is a library :wink:

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?

Yury K. 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!
(-;

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!

Yury K. 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
"Pigleg too?", week 1

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

On Mon, Feb 16, 2009 at 9:36 AM, Phlip [email protected] 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
rspec/lib/spec/matchers/wrap_expectation.rb at f6c75b1417d9178d4dcaaf9e892e23474d340ff6 · dchelimsky/rspec · GitHub,
I think it’ll solve this problem.

HTH,
David

David C. 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 rspec/lib/spec/matchers/wrap_expectation.rb at f6c75b1417d9178d4dcaaf9e892e23474d340ff6 · dchelimsky/rspec · GitHub,
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:

‘/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

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?

On Mon, Feb 16, 2009 at 8:21 PM, Phlip [email protected] wrote:

David C. 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!

./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…