我的模型中有一个名为average_n的方法。
def average_n=(average_n)
@average_n = average_n
end
我写的rspec如下
its 'should be 8' do
model.average_n=8.should==8
end
是否有其他可能的方法为此方法编写rspec?如果是,请发帖回答。
答案 0 :(得分:1)
您已经拥有了setter方法(average_n=
),因此您还需要一个getter方法:
class MyModel < ActiveRecord::Base
def average_n
@average_n
end
def average_n=(average_n)
@average_n = average_n
end
end
可以使用attr_accessor
简化:
class MyModel < ActiveRecord::Base
attr_accessor :average_n
end
如果你真的需要为它编写规范:
describe '#average_n=' do
let(:my_model) { MyModel.new }
it 'sets average_n' do
my_model.average_n = 8
expect(my_model.average_n).to eq 8
end
end