Hello, I am interested in enhancing assigns in controller specs to do things like the following assigns[:key1, :key2, :key3] #=> vals for keys Is there away to "break open" this construction to overload the :[] operator? Thanks. -Chris
One method would be to: 1. figure out the class 2. reopen the class, 3. alias the method, 4. use your method if multiple keys are given, use the old aliased method if not. Don''t know if assigns might have some metaclass trickery, though. Post what you find. I''d be interested. As for spec''ing it out, first: require File.dirname(__FILE__) + "/..." class MyController < ActionController def single_var @var = "var" end def multi_var @var1 = 1 @var2 = 2 end end describe "MyController''s single_var" do controller_name :my_controller setup do get :single_var end it "should assign one variable" do assign[:var].should == "var" end end describe "MyController''s multi_var" do ... it "should assign multi variables" do assign[:var1, :var2].should == [1, 2] end end Obviously you can use more descriptive names than I have here. But I hope you can see where I''m going with this. Scott On Apr 5, 2007, at 8:55 AM, Chris Hoffman wrote:> Hello, > I am interested in enhancing assigns in controller specs to do > things like the following > > assigns[:key1, :key2, :key3] #=> vals for keys > > Is there away to "break open" this construction to overload the :[] > operator? > > Thanks. > > -Chris > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
David Chelimsky
2007-Apr-05 19:41 UTC
[rspec-users] Extending assigns in controller specs?
On 4/5/07, Scott Taylor <mailing_lists at railsnewbie.com> wrote:> > One method would be to: > > 1. figure out the class > 2. reopen the class, > 3. alias the method, > 4. use your method if multiple keys are given, use the old aliased > method if not. > > Don''t know if assigns might have some metaclass trickery, though. > Post what you find. I''d be interested. > > As for spec''ing it out, first: > > require File.dirname(__FILE__) + "/..." > > class MyController < ActionController > def single_var > @var = "var" > end > > def multi_var > @var1 = 1 > @var2 = 2 > end > end > > describe "MyController''s single_var" do > controller_name :my_controller > > setup do > get :single_var > end > > it "should assign one variable" do > assign[:var].should == "var" > end > end > > describe "MyController''s multi_var" do > > ... > > it "should assign multi variables" do > assign[:var1, :var2].should == [1, 2] > end > endassigns is available before and after the action so you don''t really need an action to spec this: it "should assign a single var" do assigns[:a] = ''b'' assigns[:a].should == ''b'' end it "should assign multiple vars" do assigns[:a, :b] = ''c'', ''d'' assigns[:a].should == ''c'' assigns[:b].should == ''d'' end The class is IvarProxy, which is actually left over from ZenTest, which an earlier version of Spec::Rails wrapped. Cheers, David> > > Obviously you can use more descriptive names than I have here. But I > hope you can see where I''m going with this. > > Scott > > > On Apr 5, 2007, at 8:55 AM, Chris Hoffman wrote: > > > Hello, > > I am interested in enhancing assigns in controller specs to do > > things like the following > > > > assigns[:key1, :key2, :key3] #=> vals for keys > > > > Is there away to "break open" this construction to overload the :[] > > operator? > > > > Thanks. > > > > -Chris > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
That''s just the information for which I was looking. Thanks to both of you. On 4/5/07, David Chelimsky <dchelimsky at gmail.com> wrote:> On 4/5/07, Scott Taylor <mailing_lists at railsnewbie.com> wrote: > > > > One method would be to: > > > > 1. figure out the class > > 2. reopen the class, > > 3. alias the method, > > 4. use your method if multiple keys are given, use the old aliased > > method if not. > > > > Don''t know if assigns might have some metaclass trickery, though. > > Post what you find. I''d be interested. > > > > As for spec''ing it out, first: > > > > require File.dirname(__FILE__) + "/..." > > > > class MyController < ActionController > > def single_var > > @var = "var" > > end > > > > def multi_var > > @var1 = 1 > > @var2 = 2 > > end > > end > > > > describe "MyController''s single_var" do > > controller_name :my_controller > > > > setup do > > get :single_var > > end > > > > it "should assign one variable" do > > assign[:var].should == "var" > > end > > end > > > > describe "MyController''s multi_var" do > > > > ... > > > > it "should assign multi variables" do > > assign[:var1, :var2].should == [1, 2] > > end > > end > > assigns is available before and after the action so you don''t really > need an action to spec this: > > it "should assign a single var" do > assigns[:a] = ''b'' > assigns[:a].should == ''b'' > end > > it "should assign multiple vars" do > assigns[:a, :b] = ''c'', ''d'' > assigns[:a].should == ''c'' > assigns[:b].should == ''d'' > end > > The class is IvarProxy, which is actually left over from ZenTest, > which an earlier version of Spec::Rails wrapped. > > Cheers, > David > > > > > > > Obviously you can use more descriptive names than I have here. But I > > hope you can see where I''m going with this. > > > > Scott > > > > > > On Apr 5, 2007, at 8:55 AM, Chris Hoffman wrote: > > > > > Hello, > > > I am interested in enhancing assigns in controller specs to do > > > things like the following > > > > > > assigns[:key1, :key2, :key3] #=> vals for keys > > > > > > Is there away to "break open" this construction to overload the :[] > > > operator? > > > > > > Thanks. > > > > > > -Chris > > > _______________________________________________ > > > rspec-users mailing list > > > rspec-users at rubyforge.org > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Maybe Matching Threads
- reading multiple key=value pairs per line
- [PATCH: host-browser replacement 0/3] replacement of host-browser on ovirt-server
- Feature request: "database show" from manager API
- [PATCH] hivex: python: value_value no longer generates Unicode strings
- reading in a (very simple) list from a file