Hi, I have a lambda. Test = lambda { kill(333) } How should I spec if I want to make sure this Test will send kill message with 333? Thanks. Best regards, Zhi-Qiang Lei zhiqiang.lei at gmail.com
On Sun, Dec 26, 2010 at 12:05 AM, Zhi-Qiang Lei <zhiqiang.lei at gmail.com> wrote:> Hi, > > I have a lambda. > > Test = lambda { kill(333) } > > How should I spec if I want to make sure this Test will send kill message with 333? Thanks.Depends on the scope in which the block will be evaluated. Since kill is being called with no receiver, its implicit receiver will be the object in which it is evaluated, so you can mock it on that object, e.g: class Foo def bar yield end end foo = Foo.new foo.should_receive(:kill) foo.bar { kill(333) } Not sure if that aligns with your situation, but that should give you an idea. HTH, David> Best regards, > Zhi-Qiang Lei > zhiqiang.lei at gmail.com > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Hi, I''m trying to mock the scope. Test = lambda { kill(333) } app = double("test") app.should_receive(:kill).with(333).once app.instance_eval Test It will say that kill method miss in app. And to stub a kill method will not help. Can I only define a new class to test it? Thanks. On Dec 26, 2010, at 1:25 PM, David Chelimsky wrote:> On Sun, Dec 26, 2010 at 12:05 AM, Zhi-Qiang Lei <zhiqiang.lei at gmail.com> wrote: >> Hi, >> >> I have a lambda. >> >> Test = lambda { kill(333) } >> >> How should I spec if I want to make sure this Test will send kill message with 333? Thanks. > > Depends on the scope in which the block will be evaluated. Since kill > is being called with no receiver, its implicit receiver will be the > object in which it is evaluated, so you can mock it on that object, > e.g: > > class Foo > def bar > yield > end > end > > foo = Foo.new > foo.should_receive(:kill) > foo.bar { kill(333) } > > Not sure if that aligns with your situation, but that should give you an idea. > > HTH, > David > >> Best regards, >> Zhi-Qiang Lei >> zhiqiang.lei at gmail.com >> >> _______________________________________________ >> 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-usersBest regards, Zhi-Qiang Lei zhiqiang.lei at gmail.com
Sorry, it is my codes'' fault. It works now. On Dec 26, 2010, at 3:47 PM, Zhi-Qiang Lei wrote:> Hi, > > I''m trying to mock the scope. > > Test = lambda { kill(333) } > app = double("test") > app.should_receive(:kill).with(333).once > app.instance_eval Test > > It will say that kill method miss in app. And to stub a kill method will not help. Can I only define a new class to test it? Thanks. > > On Dec 26, 2010, at 1:25 PM, David Chelimsky wrote: > >> On Sun, Dec 26, 2010 at 12:05 AM, Zhi-Qiang Lei <zhiqiang.lei at gmail.com> wrote: >>> Hi, >>> >>> I have a lambda. >>> >>> Test = lambda { kill(333) } >>> >>> How should I spec if I want to make sure this Test will send kill message with 333? Thanks. >> >> Depends on the scope in which the block will be evaluated. Since kill >> is being called with no receiver, its implicit receiver will be the >> object in which it is evaluated, so you can mock it on that object, >> e.g: >> >> class Foo >> def bar >> yield >> end >> end >> >> foo = Foo.new >> foo.should_receive(:kill) >> foo.bar { kill(333) } >> >> Not sure if that aligns with your situation, but that should give you an idea. >> >> HTH, >> David >> >>> Best regards, >>> Zhi-Qiang Lei >>> zhiqiang.lei at gmail.com >>> >>> _______________________________________________ >>> 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 > > > Best regards, > Zhi-Qiang Lei > zhiqiang.lei at gmail.com >Best regards, Zhi-Qiang Lei zhiqiang.lei at gmail.com