开发者

rspec backtrace, but no error with @model.destroy for an activerecord model

开发者 https://www.devze.com 2023-02-13 02:03 出处:网络
I am trying an rspec test where the example looks like so: it \"cannot be deleted if it has children\" do

I am trying an rspec test where the example looks like so:

it "cannot be deleted if it has children" do
  children = generate_children
  children.each do |child|
    child.parent = @subj
    @subj.save
    child.save
    @subj.destroy.should be_false
    @subj.should have(1).error
   end
end

I get this in the backtrace:

/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-expectations-2.5.0/lib/rspec/expectations/fail_with.rb:29:in `fail_with'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-expectations-2.5.0/lib/rspec/expectations/handler.rb:21:in `handle_matcher'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-expectations-2.5.0/lib/rspec/expectations/extensions/kernel.rb:27:in `should'
./spec/models/my_examples.rb:14
./spec/models/my_examples.rb:10:in `each'
./spec/models/my_examples.rb:10
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:49:in `instance_eval'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:49:in `run'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:106:in `with_around_hooks'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:46:in `run'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:99:in `with_pending_capture'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:98:in `catch'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gem开发者_开发百科s/rspec-core-2.5.1/lib/rspec/core/example.rb:98:in `with_pending_capture'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:45:in `run'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:262:in `run_examples'
/opt/tms/bin/jruby-1.5.1/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:258:in `map'

But the test passes, although with the ugly backtrace shown above. Any idea how I could rewrite this test / am I doing something wrong here?


line14 is the destroy-line - which is interesting.

According to the source code, destroy doesn't return true/false - it returns the same item (through freeze).

I don't think you're expected to check if it was "true". It's probably failing because an frozen Active Record isn't false... but it's kind of a weird thing to compare with.

Not sure how you'd go about checking it...perhaps with the equivalent of

lambda do @subj.destroy end.should_not change(Subject, :count).by(-1)
0

精彩评论

暂无评论...
验证码 换一张
取 消