How do I pass form values from POST controller specs?

aslak hellesoy
Fri, 18 Jan 2008 11:55:04 -0000

On Jan 18, 2008 12:11 PM, Daniel N <has.sox at gmail.com>
wrote:
>
>
>
>
> On Jan 18, 2008 9:48 PM, aslak hellesoy <aslak.hellesoy at gmail.com>
wrote:
> >
> > On Jan 18, 2008 11:16 AM, Daniel N <has.sox at gmail.com> wrote:
> > > Sorry Aslak Gmail fired before I was ready.
> > >
> > > There was a bug in the 0.5.2 helper that has been fixed regarding
making
> the
> > > parameters available.
> > >
> > > The parameters are given to the request in encoded form however,
and
> made
> > > available as a Mash, which is similar to a
HashWithIndifferentAccess.
> > >
> > > The information here is pretty specific to Merb 0.5.3 since there
is
> > > significant changes to 0.9 in terms of hooking ability to allow
specs to
> get
> > > into the request cycle and set these things up nicely.
> > >
> > > I''ve made suggestions to your spec to get it to pass
inline.
> > >
> >
> > Thanks a lot Daniel, your advice and trunk got me a lot further.
> >
> >
> > >
> > > On Jan 18, 2008 8:24 PM, aslak hellesoy < aslak.hellesoy at
gmail.com>
> wrote:
> > >
> > > > Hi there
> > > >
> > > > I just started my first Merb app and I''m running
into some weird
> > > > things in Specs. When I do:
> > > >
> > > >  describe "create action" do
> > > >    it "should create a new project" do
> > > >      project_params = {:name =>
''bekkunin''}
> > > >      Project.should_receive (:create).with(project_params)
> > > >      controller, action =
post(''/projects'', :project =>
> project_params)
> > > >    end
> > > >  end
> > > >
> > >
> > > Once your running on 0.5.3 trunk, you can use your spec here with
one
> > > change.  Because the params hash is converted to a request, and
then
> into a
> > > Mash, you should use
> > >
> > >
> > >   describe "create action" do
> > >     it "should create a new project" do
> > >       project_params = {''name'' =>
''bekkunin''}
> > >       Project.should_receive(:create).with(project_params)
> > >       controller, action = post(''/projects'',
:project => project_params)
> > >     end
> > >   end
> > >
> >
> > Cool. IIR Rails::Spec has (or at least had) the same issue where you
> > need to use string keys.
> >
> >
> > > >
> > > > Merb::Test::FakeRequest#with (and #new) invoke
> > > > Hash#environmentize_keys! on the hash I send in as the 2nd
arg to
> > > > post, and this seems very odd to me. Why is it upcasing the
keys in
> > > > the hash *I* send in from my spec? In my controller action,
I try to
> > > > access params[:project], but it''s nil. I tried this
instead to work
> > > > around it:
> > >
> > > At the moment it is difficult to pass in the params hash directly
as you
> > > input it, so it is encoded into the request, and then handled as
per a
> > > normal request.  Again, this will probably not need to be so in
0.9
> > >
> >
> > Cool
> >
> >
> > > >
> > > >
> > > >  describe "create action" do
> > > >    it "should create a new project" do
> > > >      project_params = {:name =>
''bekkunin''}
> > > >      Project.should_receive(:create).with(project_params)
> > > >      req = fake_request
> > > >      req[:project] = project_params
> > > >      controller, action =
post(''/projects'', :fake_request => req)
> > > >    end
> > > >  end
> > > >
> > > > Now my action doesn''t even get called. the value of
action is
> > > > ''bad_request'', probably because the fake
request I created doesn''t
> > > > have all the right stuff.
> > >
> > > the return on controller, action is a left over basically a
legacy
> return
> > > value.  the post, get, put and delete methods set the controller
> variable so
> > > it is available through the controller method, so you can do
things like
> > >
> > > post(''/projects'')
> > > controller.params.keys.should include(''name'')
I don''t belive the
> > > :fake_request is available in the 0.5.2 gem..  It is only a new
> addition.
> > >
> >
> > Yes, I saw that later.
> >
> >
> > > >
> > > >
> > > > I can''t for the life of me grok how I send in
"form" values to a POST
> > > > action from a spec and could use some help here. If someone
can
> > > > explain to me the intended behaviour I can submit a patch. I
did a
> > > > fresh checkout and ran rake specs. Then I got a lot of
errors. Here is
> > > > the last one:
> > > >
> > > > 18)
> > > > NoMethodError in ''a merb mailer should be able to
use a different
> sendmail
> > > path''
> > > > undefined method `body='' for
#<MailFactory:0x3347af8>
> > > > /Users/aslakhellesoy/scm/merb/lib/merb/mailer.rb:82:in
`send''
> > > > /Users/aslakhellesoy/scm/merb/lib/merb/mailer.rb:82:in
`initialize''
> > > > /Users/aslakhellesoy/scm/merb/lib/merb/mailer.rb:82:in
`each''
> > > > /Users/aslakhellesoy/scm/merb/lib/merb/mailer.rb:82:in
`initialize''
> > > > ./spec/merb/mailer_spec.rb:22:in `new''
> > > > ./spec/merb/mailer_spec.rb:22:in
`setup_test_mailer''
> > > > ./spec/merb/mailer_spec.rb:81:
> > > >
> > > > Finished in 9.677867 seconds
> > > >
> > > > 821 examples, 18 failures, 2 pending
> > >
> > >
> > > I have just downloaded a clean trunk, and I have all specs
passing.  I
> don''t
> > > know what the issue is here.
> > >
> >
> > All of my spec errors are mailer related. Do I need a different
> > version of something or do I need to do some sort of config?
>
> I have this in my spec helper for the mailer.  I''m not sure that
it''s
> documented anywhere, but it should probably go in the spec helper
regardless
> of if your using a mailer or not.  That way ppl won''t get tripped
up by it.
>
> class Merb::Mailer
>   self.delivery_method = :test_send
> end
>

I meant the specs for Merb itself, not my own app.

I found out that I had to gem install mailfactory in order to be able
to run Merb''s own specs.
(This was a tiny message at the top of the output of rake spec that I
spotted by pure coincidence before the spec output flushed it upwards
in my shell).

>
>
> >
> >
> > Also, is there a reason you didn''t want to share this info
with the
> > list? Let me know if you want me to repost this to the list.
>
> I''ve posted this one to the list as well as your address.  Sorry
about that,
> I had a few issues with Gmail ;)
>
> >
> >
> >
> > > Hopefully you can help us to implement awsome spec helpers in 0.9
:)
> > >
> >
> > If I have time :-)
> >
> > Is there a simple way to install the Merb sources "under" my
Merb app?
> > I''d like to use piston to patch Merb within my team so that
we can get
> > work done rather than sharing patches :-)
>
> You can freeze your version of merb by using the rake tasks
>
> rake merb:freeze
> rake merb:freeze_from_svn
> rake merb:unfreeze
>
> all this really does is put the contents of trunk/lib into the framework
> directory of your app root.
> (pistons a bit rusty but I think)
> piston import http://svn.devjavu.com/merb/trunk/lib framework
>

I ended up doing:
piston import http://svn.devjavu.com/merb/trunk framework
And then change the 1st line of my app''s config/boot.rb line to
frozen_framework_path = File.join(File.dirname(__FILE__), "..",
"framework", "lib")

I recommend you make this the default. People are going to want to
check out the entire Merb so they can run Merb''s own specs after
making tweaks to it. (People freeze a framework both to secure the app
and to fix bugs in the framwork).

Now I''m running into a new problem. I''m using a frozen Merb
core and
ActiveRecord, so I need to use merb_activerecord. I cannot install
merb_activerecord as a gem, because the gem wants Merb 0.5.2 which is
not the core I want. So I don''t have any global merb related gems
installed. I''ve tried this:

piston import http://svn.devjavu.com/merb/plugins/merb_activerecord
gems/gems/merb_activerecord
(I guess I need the trunk of this since I have the trunk of core)

However, Merb is unable to find merb_activerecord:

rake specs # from my app root
rake aborted!
no such file to load -- merb_activerecord

When I decide to use Merb core"edge", what''s the best way to
also get
Merb ActiveRecord "edge"?

Aslak

> Also if you install your gems into the gems directory of your app root, you
> can lock specific gems too.
>
> HTH
> Daniel
>
>
>
> >
> >
> > Cheers,
> > Aslak
> >
> >
> >
> >
> > > HTH
> > > -Daniel
> > >
> > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > My environment:
> > > >
> > > > * OS X Tiger
> > > > * ruby 1.8.6 (2007-09-24 patchlevel 111) [
i686-darwin8.11.1]
> > > > Gems:
> > > > abstract (1.0.0)
> > > > actionmailer (2.0.2)
> > > > actionpack (2.0.2)
> > > > activerecord (2.0.2)
> > > > activeresource ( 2.0.2)
> > > > activesupport (2.0.2, 2.0.1)
> > > > archive-tar-minitar (0.5.1)
> > > > builder (2.1.2 )
> > > > cgi_multipart_eof_fix (2.5.0)
> > > > coderay ( 0.7.4.215)
> > > > color (1.4.0)
> > > > daemons (1.0.9)
> > > > diff-lcs (1.1.2)
> > > > directory_watcher (1.1.0)
> > > > erubis (2.4.1)
> > > > eventmachine ( 0.10.0)
> > > > fastthread ( 1.0.1)
> > > > fcgi (0.8.7)
> > > > flexmock (0.8.0)
> > > > gem_plugin (0.2.3)
> > > > god (0.6.0)
> > > > GraphvizR (0.5.1)
> > > > gratr (0.4.3)
> > > > haml (1.7.2)
> > > > heckle ( 1.4.1)
> > > > heel (0.6.0)
> > > > highline (1.4.0)
> > > > hoe (1.4.0 , 1.3.0)
> > > > hpricot (0.6)
> > > > json (1.1.2)
> > > > json_pure (1.1.2)
> > > > launchy (0.3.1)
> > > > libxml-ruby ( 0.5.2.0)
> > > > liquid (1.7.0)
> > > > logging (0.5.3)
> > > > markaby (0.5)
> > > > memcache-client ( 1.5.0)
> > > > merb (0.5.2)
> > > > merb_activerecord ( 0.5)
> > > > meta_project (0.4.15)
> > > > mime-types (1.15)
> > > > mocha (0.5.5)
> > > > mongrel (1.1.3, 1.1.1)
> > > > mongrel_cluster (1.0.5)
> > > > needle (1.3.0)
> > > > net-sftp ( 1.1.0)
> > > > net-ssh (1.1.2 )
> > > > newgem (0.16.1)
> > > > oniguruma (1.1.0)
> > > > open4 (0.9.6)
> > > > ParseTree (2.1.1, 2.0.2)
> > > > pdf-storycards (0.0.1)
> > > > pdf-writer ( 1.1.7, 1.1.4)
> > > > piston (1.3.3)
> > > > Platform (0.4.0)
> > > > plist (3.0.0)
> > > > rack (0.2.0)
> > > > rails ( 2.0.2)
> > > > rake (0.8.1)
> > > > rb-appscript (0.4.0)
> > > > rcov ( 0.8.1.2.0)
> > > > RedCloth (3.0.4)
> > > > rspec (1.1.1)
> > > > rubigen (1.1.1)
> > > > ruby-growl (1.0.1)
> > > > ruby2ruby (1.1.8, 1.1.7)
> > > > rubyforge (0.4.4)
> > > > rubygems-update ( 1.0.1)
> > > > RubyInline (3.6.6, 3.6.5)
> > > > rubyworks_app (0.0.1)
> > > > safariwatir (0.2.5)
> > > > sinatra (0.1.7)
> > > > sqlite3-ruby (1.2.1)
> > > > syntax (1.0.0)
> > > > textpow ( 0.10.0)
> > > > thin (0.5.0)
> > > > tidy (1.1.2)
> > > > transaction-simple (1.4.0)
> > > > tzinfo ( 0.3.5)
> > > > ultraviolet (0.10.1)
> > > > uuidtools (1.0.2)
> > > > vlad (1.1.0)
> > > > webby (0.6.0)
> > > > ZenTest (3.6.1)
> > > >
> > > > Am I doing something obviously wrong?
> > > >
> > > > Cheers,
> > > > Aslak
> > > > _______________________________________________
> > > >
> > > > Merb-devel mailing list
> > > > Merb-devel at rubyforge.org
> > > > http://rubyforge.org/mailman/listinfo/merb-devel
> > > >
> > >
> > >
> >
>
>