我有一个带有一个方向盘的模型和一个相关的steering_wheel_color方法:
class Car
has_one :steering_wheel
validates :steering_wheel, presence: true
def steering_wheel_color
self.steering_wheel.color
end
end
当我测试此模型以确保方向盘存在时,它会因为steering_wheel_color方法未定义而失败:
describe "Car" do
@car = Car.new(blah blah)
describe "when the steering wheel is not present"
before { @car.steering_wheel_id = "" }
it "won't be valid" do
@car.valid?.must_equal false
end
end
结果如下:
NoMethodError: undefined method `color=' for #<SteeringWheel:0x000001092f9388>
如果没有因为方法而导致我的测试失败,我如何测试关联的存在(而不仅仅是外键的存在?)