Mocking a Rails application object with mocha
-
16-12-2020 - |
Frage
I'm writing a generator and I need to get mock a Rails.application object and get back the Rails.application.class.parent as the name of the Rails application.
def test_model_with_application_namespace
name = "Dummyapp"
application = Rails.stubs(:application).class.parent.returns(name)
run_generator ["file", "--namespaced"]
assert_file "app/models/myapp/file.rb", /class Dummyapp::File < ActiveRecord::Base/
end
This is what I have so far for my test.
Lösung
You need the object retured by Rails.application.class
to be a mock that responds to parent
and returns name
. Right now, you just stub out application. You need parent, class, and application to be mocks. There is probably a cleaner way of doing it, but I think this will do what you want:
application = Rails.stubs(:application).returns(mock(:class => mock(:parent => name)))
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow