niku -E:)
2011-Mar-15 15:57 UTC
[rspec-users] How do I use expect{}.to change().from().to() on Array?
Hi.
I''m using ruby1.9.2 and rspec2.5.1
I want to use "expect{}.to change().from().to()" like this
https://gist.github.com/870897
When Int class, it passed.
When Ary class, it failed.
Why was Ary test failed and How do I pass this test?
regards.
David Chelimsky
2011-Mar-15 16:50 UTC
[rspec-users] How do I use expect{}.to change().from().to() on Array?
On Mar 15, 2011, at 10:57 AM, niku -E:) wrote:> Hi. > > I''m using ruby1.9.2 and rspec2.5.1 > I want to use "expect{}.to change().from().to()" like this > > https://gist.github.com/870897 > > When Int class, it passed. > When Ary class, it failed. > > Why was Ary test failed and How do I pass this test?subject.ary returns the same object both times, whereas subject.count returns different objects each time. Make sense?
Justin Ko
2011-Mar-15 17:08 UTC
[rspec-users] How do I use expect{}.to change().from().to() on Array?
On Tue, Mar 15, 2011 at 8:57 AM, niku -E:) <niku at niku.name> wrote:> Hi. > > I''m using ruby1.9.2 and rspec2.5.1 > I want to use "expect{}.to change().from().to()" like this > > https://gist.github.com/870897 > > When Int class, it passed. > When Ary class, it failed. > > Why was Ary test failed and How do I pass this test? > > regards. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >Your Ary @ary variable is initialized as "[]". So of course calling increment on it will change it to "[0]". For your spec to pass, initialize it as "[0]" -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110315/9ebc463d/attachment.html>
Justin Ko
2011-Mar-15 17:41 UTC
[rspec-users] How do I use expect{}.to change().from().to() on Array?
On Tue, Mar 15, 2011 at 10:08 AM, Justin Ko <jko170 at gmail.com> wrote:> > > On Tue, Mar 15, 2011 at 8:57 AM, niku -E:) <niku at niku.name> wrote: > >> Hi. >> >> I''m using ruby1.9.2 and rspec2.5.1 >> I want to use "expect{}.to change().from().to()" like this >> >> https://gist.github.com/870897 >> >> When Int class, it passed. >> When Ary class, it failed. >> >> Why was Ary test failed and How do I pass this test? >> >> regards. >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > > Your Ary @ary variable is initialized as "[]". So of course calling > increment on it will change it to "[0]". For your spec to pass, initialize > it as "[0]" >I''m completely wrong. Didn''t read your code properly. Where is the delete link? :) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110315/f1d754f2/attachment.html>
niku -E:)
2011-Mar-21 01:41 UTC
[rspec-users] How do I use expect{}.to change().from().to() on Array?
I understand that "expect{}.to change()" can''t test
"destructive method".
So, I should write test like that. correct?
require ''rspec''
class Ary
attr_reader :ary
def initialize
@count = 0
@ary = []
increment
end
def increment
@ary << @count
@count += 1
end
end
describe Ary, "#ary" do
subject{ Ary.new }
it "should increment the ary" do
# expect { subject.increment }.to change(subject, :ary).from([]).to([0])
subject.ary.should == [0]
subject.increment
subject.ary.should == [0,1]
end
end
On Wed, Mar 16, 2011 at 2:41 AM, Justin Ko <jko170 at gmail.com>
wrote:>
>
> On Tue, Mar 15, 2011 at 10:08 AM, Justin Ko <jko170 at gmail.com>
wrote:
>>
>>
>> On Tue, Mar 15, 2011 at 8:57 AM, niku -E:) <niku at niku.name>
wrote:
>>>
>>> Hi.
>>>
>>> I''m using ruby1.9.2 and rspec2.5.1
>>> I want to use "expect{}.to change().from().to()" like
this
>>>
>>> https://gist.github.com/870897
>>>
>>> When Int class, it passed.
>>> When Ary class, it failed.
>>>
>>> Why was Ary test failed and How do I pass this test?
>>>
>>> regards.
>>> _______________________________________________
>>> rspec-users mailing list
>>> rspec-users at rubyforge.org
>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>
>> Your Ary @ary variable is initialized as "[]". So of course
calling
>> increment on it will change it to "[0]". For your spec to
pass, initialize
>> it as "[0]"
>
> I''m completely wrong. Didn''t read your code properly.
Where is the delete
> link? :)
niku -E:)
2011-Mar-21 01:54 UTC
[rspec-users] How do I use expect{}.to change().from().to() on Array?
Sorry. Test comment is incorrect.
This is intentional test.
require ''rspec''
class Ary
attr_reader :ary
def initialize
@count = 0
@ary = []
increment
end
def increment
@ary << @count
@count += 1
end
end
describe Ary, "#ary" do
subject{ Ary.new }
it "should increment the ary" do
# expect { subject.increment }.to change(subject, :ary).from([0]).to([0,1])
subject.ary.should == [0]
subject.increment
subject.ary.should == [0,1]
end
end
On Mon, Mar 21, 2011 at 10:41 AM, niku -E:) <niku at niku.name>
wrote:> I understand that "expect{}.to change()" can''t test
"destructive method".
> So, I should write test like that. correct?
>
> require ''rspec''
>
> class Ary
> ?attr_reader :ary
> ?def initialize
> ? ?@count = 0
> ? ?@ary = []
> ? ?increment
> ?end
> ?def increment
> ? ?@ary << @count
> ? ?@count += 1
> ?end
> end
>
> describe Ary, "#ary" do
> ?subject{ Ary.new }
> ?it "should increment the ary" do
> ? ?# expect { subject.increment }.to change(subject, :ary).from([]).to([0])
> ? ?subject.ary.should == [0]
> ? ?subject.increment
> ? ?subject.ary.should == [0,1]
> ?end
> end
>
> On Wed, Mar 16, 2011 at 2:41 AM, Justin Ko <jko170 at gmail.com>
wrote:
>>
>>
>> On Tue, Mar 15, 2011 at 10:08 AM, Justin Ko <jko170 at gmail.com>
wrote:
>>>
>>>
>>> On Tue, Mar 15, 2011 at 8:57 AM, niku -E:) <niku at
niku.name> wrote:
>>>>
>>>> Hi.
>>>>
>>>> I''m using ruby1.9.2 and rspec2.5.1
>>>> I want to use "expect{}.to change().from().to()" like
this
>>>>
>>>> https://gist.github.com/870897
>>>>
>>>> When Int class, it passed.
>>>> When Ary class, it failed.
>>>>
>>>> Why was Ary test failed and How do I pass this test?
>>>>
>>>> regards.
>>>> _______________________________________________
>>>> rspec-users mailing list
>>>> rspec-users at rubyforge.org
>>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>>
>>> Your Ary @ary variable is initialized as "[]". So of
course calling
>>> increment on it will change it to "[0]". For your spec to
pass, initialize
>>> it as "[0]"
>>
>> I''m completely wrong. Didn''t read your code properly.
Where is the delete
>> link? :)
>
niku -E:)
2011-Mar-25 23:43 UTC
[rspec-users] How do I use expect{}.to change().from().to() on Array?
finally, I solved this code.
require ''rspec''
class Ary
attr_reader :ary
def initialize
@count = 0
@ary = []
increment
end
def increment
@ary << @count
@count += 1
end
end
describe Ary, "#ary" do
subject{ Ary.new }
it "should increment the ary" do
# expect { subject.increment }.to change(subject,
:ary).from([0]).to([0,1]) # fail
expect { subject.increment }.to change{ subject.ary.clone
}.from([0]).to([0,1])
end
it "should increment the ary by [1]" do
expect { subject.increment }.to change{ subject.ary.clone }.by([1])
end
end
On Mon, Mar 21, 2011 at 10:54 AM, niku -E:) <niku at niku.name>
wrote:> Sorry. Test comment is incorrect.
> This is intentional test.
>
> require ''rspec''
>
> class Ary
> ?attr_reader :ary
> ?def initialize
> ? ?@count = 0
> ? ?@ary = []
> ? ?increment
> ?end
> ?def increment
> ? ?@ary << @count
> ? ?@count += 1
> ?end
> end
>
> describe Ary, "#ary" do
> ?subject{ Ary.new }
> ?it "should increment the ary" do
> ? ?# expect { subject.increment }.to change(subject,
:ary).from([0]).to([0,1])
> ? ?subject.ary.should == [0]
> ? ?subject.increment
> ? ?subject.ary.should == [0,1]
> ?end
> end
>
> On Mon, Mar 21, 2011 at 10:41 AM, niku -E:) <niku at niku.name>
wrote:
>> I understand that "expect{}.to change()" can''t test
"destructive method".
>> So, I should write test like that. correct?
>>
>> require ''rspec''
>>
>> class Ary
>> ?attr_reader :ary
>> ?def initialize
>> ? ?@count = 0
>> ? ?@ary = []
>> ? ?increment
>> ?end
>> ?def increment
>> ? ?@ary << @count
>> ? ?@count += 1
>> ?end
>> end
>>
>> describe Ary, "#ary" do
>> ?subject{ Ary.new }
>> ?it "should increment the ary" do
>> ? ?# expect { subject.increment }.to change(subject,
:ary).from([]).to([0])
>> ? ?subject.ary.should == [0]
>> ? ?subject.increment
>> ? ?subject.ary.should == [0,1]
>> ?end
>> end
>>
>> On Wed, Mar 16, 2011 at 2:41 AM, Justin Ko <jko170 at gmail.com>
wrote:
>>>
>>>
>>> On Tue, Mar 15, 2011 at 10:08 AM, Justin Ko <jko170 at
gmail.com> wrote:
>>>>
>>>>
>>>> On Tue, Mar 15, 2011 at 8:57 AM, niku -E:) <niku at
niku.name> wrote:
>>>>>
>>>>> Hi.
>>>>>
>>>>> I''m using ruby1.9.2 and rspec2.5.1
>>>>> I want to use "expect{}.to change().from().to()"
like this
>>>>>
>>>>> https://gist.github.com/870897
>>>>>
>>>>> When Int class, it passed.
>>>>> When Ary class, it failed.
>>>>>
>>>>> Why was Ary test failed and How do I pass this test?
>>>>>
>>>>> regards.
>>>>> _______________________________________________
>>>>> rspec-users mailing list
>>>>> rspec-users at rubyforge.org
>>>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>>>
>>>> Your Ary @ary variable is initialized as "[]". So of
course calling
>>>> increment on it will change it to "[0]". For your
spec to pass, initialize
>>>> it as "[0]"
>>>
>>> I''m completely wrong. Didn''t read your code
properly. Where is the delete
>>> link? :)
>>
>