In rspec, you often want to mock a method to make it easier to test.
In the example below, the set_complex_config
method affects the ʻadmin?` method.
The set_complex_config
method already has its own test code and we only want the response.
#Code example is appropriate
class User
def admin?
res = set_complex_config
case res
when 'hoge'
'hoge'
when 'fuga'
'fuga'
end
end
def set_complex_config
#Complex processing
end
end
I can think of two ways to mock in such a case, so I thought about which one was better.
How to overwrite the set_complex_config method of User class with singleton method as shown below.
let(:hoge_let) { 'hoge' }
it do
user = create(:user)
hoge_var = 'hoge'
user.define_singleton_method(:set_complex_config) { hoge_var }
user.set_complex_config =>Move
user.define_singleton_method(:set_complex_config) { hoge_let }
user.set_complex_config => wrong number of arguments (given 0, expected 2..3)
end
At first glance, it looks cool, but in this case, inside the example or before block, the block of the define_singleton_method method can't read the let variable.
The allow method works fine.
let(:hoge_let) { 'hoge' }
it do
user = create(:user)
allow(user).to receive(:attributes).and_return(hoge_let)
user.set_complex_config =>Move
end
It seems better to use ʻallow (). To receive (). And_return ()` obediently.
Recommended Posts