开发者

Rails model clash with RSpec

开发者 https://www.devze.com 2023-02-21 05:36 出处:网络
I\'m trying to get the RSpec-tests of my Rails application back to work. As far as I can tell, the only real difference between when they were green and now is that I\'m now using ruby 1.9, whereas th

I'm trying to get the RSpec-tests of my Rails application back to work. As far as I can tell, the only real difference between when they were green and now is that I'm now using ruby 1.9, whereas they used to pass with ruby 1.8.7.

I have a model

class Change < ActiveRecord::Base
...
end

which is used a spec:

describe ChangeObserver do
  let (:c) { Change.new(:comment => "Test", :originator => "x.y")}
  it "finds affected modules for a change" do
    c.should_receive(:affected).and_return([])
    c.save
  end
end

(yes, I need a Change instance for testing the observer).

These specs fail with:

1) ChangeObserver finds affected modules for a change
   Failure/Error: c.save
   NoMethodError:
     undefined method `save' for #<RSpec::Matchers::C开发者_运维技巧hange:0x3c8e5f0>

So obviously my Change class clashes with [RSpec::Matchers::Change][1], but it didn't do that all the time (I am sure it worked with ruby 1.8.7). Is there something different to the way ruby loads modules in 1.9? How can I require my own Change class (note: it is not inside a module, so I don't know how to qualify it).


Use ::Change to denote the toplevel namespace, since RSpec's Change class is within the module RSpec::Matchers. As so:

describe ChangeObserver do
  let (:c) { ::Change.new(:comment => "Test", :originator => "x.y")}
  it "finds affected modules for a change" do
    c.should_receive(:affected).and_return([])
    c.save
  end
end
0

精彩评论

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