Rodrigo Rosenfeld Rosas
2009-Dec-08 22:04 UTC
[rspec-users] Custom Matcher and Webrat methods
I would like to be able to write a custom matcher so that I could call this test, for instance: @user.should be_allowed_to_visit(url) @non_welcome.should_not be_allowed_to_visit(url) The matcher would call Webrat methods such as ''visit''. The problem is that it would try to call ''visit'' from User class instead of the binding in which @user access is verified... I have manually set the binding to the Matcher in a before(:all), to avoid calling ''be_allowed_to_visit(url, binding)'', but I would like to know if there would be a better way to "get" this binding... Any thoughts on that? Thanks, Rodrigo. __________________________________________________ Fa?a liga??es para outros computadores com o novo Yahoo! Messenger http://br.beta.messenger.yahoo.com/
On 8 Dec 2009, at 22:04, Rodrigo Rosenfeld Rosas wrote:> I would like to be able to write a custom matcher so that I could > call this test, for instance: > > @user.should be_allowed_to_visit(url) > @non_welcome.should_not be_allowed_to_visit(url) > > The matcher would call Webrat methods such as ''visit''. > > The problem is that it would try to call ''visit'' from User class > instead of the binding in which @user access is verified... > > I have manually set the binding to the Matcher in a before(:all), to > avoid calling ''be_allowed_to_visit(url, binding)'', but I would like > to know if there would be a better way to "get" this binding... > > Any thoughts on that?How have you defined the matcher? If you use the old-school technique where you create a matcher class and a helper method to construct the class (instead of using the shiny new matcher DSL) then the method should execute in the context that can see the binding, so you can pick it up and pass it into the matcher class at that point. Make sense? cheers, Matt http://mattwynne.net +447974 430184
Rodrigo Rosenfeld Rosas
2009-Dec-10 00:53 UTC
[rspec-users] Custom Matcher and Webrat methods
Matt Wynne escreveu:> > On 8 Dec 2009, at 22:04, Rodrigo Rosenfeld Rosas wrote: > >> I would like to be able to write a custom matcher so that I could >> call this test, for instance: >> >> @user.should be_allowed_to_visit(url) >> @non_welcome.should_not be_allowed_to_visit(url) >> >> The matcher would call Webrat methods such as ''visit''. >> >> The problem is that it would try to call ''visit'' from User class >> instead of the binding in which @user access is verified... >> >> I have manually set the binding to the Matcher in a before(:all), to >> avoid calling ''be_allowed_to_visit(url, binding)'', but I would like >> to know if there would be a better way to "get" this binding... >> >> Any thoughts on that? > > How have you defined the matcher? If you use the old-school technique > where you create a matcher class and a helper method to construct the > class (instead of using the shiny new matcher DSL) then the method > should execute in the context that can see the binding, so you can > pick it up and pass it into the matcher class at that point. > > Make sense?Thank you, Matt, I have finally got it working. The trick is passing self to the initialize of the matcher class like: def be_allowed_to_visit(url) BeAllowedToVisit.new(url, self) # def initialize(url, scope); scope.visit url...;end end One problem less :) Next one to be solved: making Selenium work faster with Webrat :) Thanks, Rodrigo. __________________________________________________ Fa?a liga??es para outros computadores com o novo Yahoo! Messenger http://br.beta.messenger.yahoo.com/
On 10 Dec 2009, at 00:53, Rodrigo Rosenfeld Rosas wrote:> Matt Wynne escreveu: >> >> On 8 Dec 2009, at 22:04, Rodrigo Rosenfeld Rosas wrote: >> >>> I would like to be able to write a custom matcher so that I could >>> call this test, for instance: >>> >>> @user.should be_allowed_to_visit(url) >>> @non_welcome.should_not be_allowed_to_visit(url) >>> >>> The matcher would call Webrat methods such as ''visit''. >>> >>> The problem is that it would try to call ''visit'' from User class >>> instead of the binding in which @user access is verified... >>> >>> I have manually set the binding to the Matcher in a before(:all), >>> to avoid calling ''be_allowed_to_visit(url, binding)'', but I would >>> like to know if there would be a better way to "get" this binding... >>> >>> Any thoughts on that? >> >> How have you defined the matcher? If you use the old-school >> technique where you create a matcher class and a helper method to >> construct the class (instead of using the shiny new matcher DSL) >> then the method should execute in the context that can see the >> binding, so you can pick it up and pass it into the matcher class >> at that point. >> >> Make sense? > Thank you, Matt, I have finally got it working. The trick is passing > self to the initialize of the matcher class like: > > def be_allowed_to_visit(url) > BeAllowedToVisit.new(url, self) # def initialize(url, scope); > scope.visit url...;end > end > > One problem less :) Next one to be solved: making Selenium work > faster with Webrat :)You might want to look at replacing webrat with Capybara then. I''ve not tried it myself, but I understand Selenium2 (which Capybara supports) is much faster.> > Thanks, > > Rodrigo. > > __________________________________________________ > Fa?a liga??es para outros computadores com o novo Yahoo! Messenger http://br.beta.messenger.yahoo.com/ > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-userscheers, Matt +447974 430184 matt at mattwynne.net http://mattwynne.net
Rodrigo Rosenfeld Rosas
2009-Dec-10 10:51 UTC
[rspec-users] Custom Matcher and Webrat methods
Matt Wynne escreveu:> > On 10 Dec 2009, at 00:53, Rodrigo Rosenfeld Rosas wrote: > >> Matt Wynne escreveu: >>> >>> ... >> >> One problem less :) Next one to be solved: making Selenium work >> faster with Webrat :) > > You might want to look at replacing webrat with Capybara then. I''ve > not tried it myself, but I understand Selenium2 (which Capybara > supports) is much faster.Thank you once again Matt. I wonder how you get updated by these new gems... :) I''ll give Capybara a try. It seems well documented and also the Webrat list is almost dead lately... Only a few questions and no answer lately... Thank you for the suggestion. Best Regards, Rodrigo. __________________________________________________ Fa?a liga??es para outros computadores com o novo Yahoo! Messenger http://br.beta.messenger.yahoo.com/