Skip to content

Commit

Permalink
[ci] rspec: Replace mocha with rspec-mocks
Browse files Browse the repository at this point in the history
Once the old test suite is gone, we can even remove the mocha gem
dependency.

https://relishapp.com/rspec/rspec-mocks/v/3-5/docs/basics
https://github.com/rspec/rspec-mocks
  • Loading branch information
bgeuken committed Nov 1, 2016
1 parent 5c55189 commit b6828ae
Show file tree
Hide file tree
Showing 17 changed files with 59 additions and 58 deletions.
14 changes: 7 additions & 7 deletions src/api/spec/controllers/webui/package_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@

context 'with a failure in the backend' do
before do
Buildresult.stubs(:find_hashed).raises(ActiveXML::Transport::Error, 'fake message')
allow(Buildresult).to receive(:find_hashed).and_raise(ActiveXML::Transport::Error, 'fake message')
post :binaries, params: { package: source_package, project: source_project, repository: repo_for_source_project }
end

Expand All @@ -229,7 +229,7 @@

context 'without build results' do
before do
Buildresult.stubs(:find_hashed).returns(nil)
allow(Buildresult).to receive(:find_hashed)
post :binaries, params: { package: source_package, project: source_project, repository: repo_for_source_project }
end

Expand All @@ -241,7 +241,7 @@
render_views

before do
Buildresult.stubs(:find).returns(fake_build_results_without_binaries)
allow(Buildresult).to receive(:find).and_return(fake_build_results_without_binaries)
post :binaries, params: { package: source_package, project: source_project, repository: repo_for_source_project }
end

Expand All @@ -253,7 +253,7 @@
render_views

before do
Buildresult.stubs(:find).returns(fake_build_results)
allow(Buildresult).to receive(:find).and_return(fake_build_results)
post :binaries, params: { package: source_package, project: source_project, repository: repo_for_source_project }
end

Expand Down Expand Up @@ -417,7 +417,7 @@
describe "DELETE #remove_file" do
before do
login(user)
Package.any_instance.stubs(:delete_file).returns(true)
allow_any_instance_of(Package).to receive(:delete_file).and_return(true)
end

def remove_file_post
Expand All @@ -437,7 +437,7 @@ def remove_file_post

context "with not successful backend call" do
before do
Package.any_instance.stubs(:delete_file).raises(ActiveXML::Transport::NotFoundError)
allow_any_instance_of(Package).to receive(:delete_file).and_raise(ActiveXML::Transport::NotFoundError)
remove_file_post
end

Expand All @@ -452,7 +452,7 @@ def remove_file_post
end

it "calls delete_file method" do
Package.any_instance.expects(:delete_file).with('the_file')
allow_any_instance_of(Package).to receive(:delete_file).with('the_file')
remove_file_post
end
end
Expand Down
16 changes: 8 additions & 8 deletions src/api/spec/controllers/webui/patchinfo_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def do_proper_post_save

context 'when it fails to create the patchinfo package' do
before do
Patchinfo.any_instance.stubs(:create_patchinfo).returns(false)
allow_any_instance_of(Patchinfo).to receive(:create_patchinfo).and_return(false)
post :new_patchinfo, params: { project: user.home_project }
end

Expand All @@ -78,7 +78,7 @@ def do_proper_post_save

context 'when the patchinfo package file is not found' do
before do
Package.any_instance.stubs(:patchinfo).returns(nil)
allow_any_instance_of(Package).to receive(:patchinfo)
post :new_patchinfo, params: { project: user.home_project }
end

Expand All @@ -88,8 +88,8 @@ def do_proper_post_save

context 'when is successfull creating the patchinfo package' do
before do
Buildresult.stubs(:find).returns(fake_build_results)
Package.any_instance.stubs(:patchinfo).returns(fake_patchinfo_with_binaries)
allow(Buildresult).to receive(:find).and_return(fake_build_results)
allow_any_instance_of(Package).to receive(:patchinfo).and_return(fake_patchinfo_with_binaries)
post :new_patchinfo, params: { project: user.home_project }
end

Expand Down Expand Up @@ -117,7 +117,7 @@ def do_proper_post_save

context 'with a valid patchinfo' do
it 'updates and redirects to edit_patchinfo' do
Patchinfo.any_instance.expects(:cmd_update_patchinfo).with(user.home_project_name, patchinfo_package.name)
expect_any_instance_of(Patchinfo).to receive(:cmd_update_patchinfo).with(user.home_project_name, patchinfo_package.name)
post :updatepatchinfo, params: { project: user.home_project_name, package: patchinfo_package.name }
expect(response).to redirect_to(action: 'edit_patchinfo', project: user.home_project_name, package: patchinfo_package.name)
end
Expand Down Expand Up @@ -218,7 +218,7 @@ def do_proper_post_save
context "without permission to edit the patchinfo-file" do
before do
patchinfo_package
Suse::Backend.stubs(:put).raises(ActiveXML::Transport::ForbiddenError)
allow(Suse::Backend).to receive(:put).and_raise(ActiveXML::Transport::ForbiddenError)
do_proper_post_save
end

Expand All @@ -229,7 +229,7 @@ def do_proper_post_save
context "putting the file is taking so long that will raise a timeout" do
before do
patchinfo_package
Suse::Backend.stubs(:put).raises(Timeout::Error)
allow(Suse::Backend).to receive(:put).and_raise(Timeout::Error)
do_proper_post_save
end

Expand All @@ -254,7 +254,7 @@ def do_proper_post_save

context "if package can't be removed" do
before do
Package.any_instance.stubs(:check_weak_dependencies?).returns(false)
allow_any_instance_of(Package).to receive(:check_weak_dependencies?).and_return(false)
post :remove, params: { project: user.home_project_name, package: patchinfo_package.name }
end

Expand Down
34 changes: 17 additions & 17 deletions src/api/spec/controllers/webui/project_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@
describe 'GET #show' do
before do
# To not ask backend for build status
Project.any_instance.stubs(:number_of_build_problems).returns(0)
allow_any_instance_of(Project).to receive(:number_of_build_problems).and_return(0)
end

context 'without nextstatus param' do
Expand Down Expand Up @@ -254,9 +254,9 @@
before do
login admin_user
# Avoid fetching from backend directly
Directory.stubs(:hashed).returns(Xmlhash::XMLHash.new('entry' => {'name' => '_patchinfo'}))
allow(Directory).to receive(:hashed).and_return(Xmlhash::XMLHash.new('entry' => {'name' => '_patchinfo'}))
# Avoid writing to the backend
Package.any_instance.stubs(:sources_changed).returns(nil)
allow_any_instance_of(Package).to receive(:sources_changed)
Patchinfo.new.create_patchinfo(apache_project.name, nil, comment: 'Fake comment', force: false)
get :show, params: { project: apache_project }
end
Expand Down Expand Up @@ -360,7 +360,7 @@
it 'assigns the buildresult' do
summary = Xmlhash::XMLHash.new({'statuscount' => {'code' => 'succeeded', 'count' => 1} })
build_result = { 'result' => Xmlhash::XMLHash.new({'repository' => 'openSUSE', 'arch' => 'x86_64', 'summary' => summary }) }
Buildresult.stubs(:find_hashed).returns(Xmlhash::XMLHash.new(build_result))
allow(Buildresult).to receive(:find_hashed).and_return(Xmlhash::XMLHash.new(build_result))
get :buildresult, params: { project: project_with_package }, xhr: true
expect(assigns(:buildresult)).to match_array([["openSUSE", [["x86_64", [[:succeeded, 1]]]]]])
end
Expand All @@ -382,7 +382,7 @@

context 'with check_weak_dependencies enabled' do
before do
Project.any_instance.stubs(:check_weak_dependencies?).returns(true)
allow_any_instance_of(Project).to receive(:check_weak_dependencies?).and_return(true)
end

context 'having a parent project' do
Expand All @@ -409,7 +409,7 @@

context 'with check_weak_dependencies disabled' do
before do
Project.any_instance.stubs(:check_weak_dependencies?).returns(false)
allow_any_instance_of(Project).to receive(:check_weak_dependencies?).and_return(false)
delete :destroy, params: { project: user.home_project }
end

Expand All @@ -422,7 +422,7 @@
describe 'GET #rebuild_time' do
before do
# To not ask backend for build status
Project.any_instance.stubs(:number_of_build_problems).returns(0)
allow_any_instance_of(Project).to receive(:number_of_build_problems).and_return(0)
end

context 'with an invalid scheduler' do
Expand All @@ -436,8 +436,8 @@

context 'without build dependency info or jobs history' do
before do
BuilddepInfo.stubs(:find).returns(nil)
Jobhistory.stubs(:find).returns(nil)
allow(BuilddepInfo).to receive(:find)
allow(Jobhistory).to receive(:find)
get :rebuild_time, params: { project: user.home_project, repository: repo_for_user_home.name, arch: 'x86_64' }
end

Expand All @@ -447,15 +447,15 @@

context 'normal flow' do
before do
BuilddepInfo.stubs(:find).returns([])
Jobhistory.stubs(:find).returns([])
allow(BuilddepInfo).to receive(:find).and_return([])
allow(Jobhistory).to receive(:find).and_return([])
end

context 'with diststats generated' do
before do
path = Xmlhash::XMLHash.new({'package' => 'package_name' })
longestpaths_xml = Xmlhash::XMLHash.new({ 'longestpath' => Xmlhash::XMLHash.new({'path' => path }) })
Webui::ProjectController.any_instance.stubs(:call_diststats).returns(longestpaths_xml)
allow_any_instance_of(Webui::ProjectController).to receive(:call_diststats).and_return(longestpaths_xml)
get :rebuild_time, params: { project: user.home_project, repository: repo_for_user_home.name, arch: 'x86_64' }
end

Expand All @@ -464,7 +464,7 @@

context 'with diststats not generated' do
before do
Webui::ProjectController.any_instance.stubs(:call_diststats).returns(nil)
allow_any_instance_of(Webui::ProjectController).to receive(:call_diststats)
get :rebuild_time, params: { project: user.home_project, repository: repo_for_user_home.name, arch: 'x86_64' }
end

Expand Down Expand Up @@ -594,7 +594,7 @@

context "without target project" do
before do
BsRequestActionDelete.expects(:new).raises(BsRequestAction::UnknownTargetProject)
expect(BsRequestActionDelete).to receive(:new).and_raise(BsRequestAction::UnknownTargetProject)
post :remove_target_request, params: { project: apache_project, description: 'Fake description' }
end

Expand All @@ -604,7 +604,7 @@

context "without target package" do
before do
BsRequestActionDelete.expects(:new).raises(BsRequestAction::UnknownTargetPackage)
expect(BsRequestActionDelete).to receive(:new).and_raise(BsRequestAction::UnknownTargetPackage)
post :remove_target_request, params: { project: apache_project, description: 'Fake description' }
end

Expand Down Expand Up @@ -654,7 +654,7 @@
before do
request.env["HTTP_REFERER"] = root_url # Needed for the redirect_to :back
path_element # Needed before stubbing Project#valid? to false
Project.any_instance.stubs(:valid?).returns(false)
allow_any_instance_of(Project).to receive(:valid?).and_return(false)
post :remove_path_from_target, params: { project: user.home_project, repository: repo_for_user_home, path: path_element }
end

Expand Down Expand Up @@ -852,7 +852,7 @@

context "with the proper params" do
before do
BsRequest.any_instance.stubs(:save!).returns(true)
allow_any_instance_of(BsRequest).to receive(:save!).and_return(true)
post :new_incident_request, params: { project: maintenance_project, description: "Fake description for a request" }
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@
context "with a distribution called images" do
before do
login user
Project.any_instance.stubs(:prepend_kiwi_config).returns(true)
allow_any_instance_of(Project).to receive(:prepend_kiwi_config).and_return(true)
post :create_image_repository, params: { project: user.home_project }
end

Expand Down
2 changes: 1 addition & 1 deletion src/api/spec/controllers/webui/request_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@

context "a request causing a APIException" do
before do
BsRequest.any_instance.stubs(:save!).raises(APIException, "something happened")
allow_any_instance_of(BsRequest).to receive(:save!).and_raise(APIException, "something happened")
post :delete_request, params: { project: target_project, package: target_package, description: "delete it!" }
end

Expand Down
6 changes: 3 additions & 3 deletions src/api/spec/controllers/webui/search_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
context 'with search_text starting with obs://' do
context 'and a package' do
before do
Package.stubs(:exists_by_project_and_name).returns(true)
allow(Package).to receive(:exists_by_project_and_name).and_return(true)
get :index, params: { search_text: "obs://build.opensuse.org/#{user.home_project.name}/i586/1-#{package.name}" }
end

Expand Down Expand Up @@ -91,7 +91,7 @@

context 'with proper parameters but no results' do
before do
ThinkingSphinx.stubs(:search).returns([])
allow(ThinkingSphinx).to receive(:search).and_return([])
get :index, params: { search_text: 'whatever' }
end

Expand All @@ -102,7 +102,7 @@

context 'with proper parameters and some results' do
before do
ThinkingSphinx.stubs(:search).returns(["Fake result with #{package.name}"])
allow(ThinkingSphinx).to receive(:search).and_return(["Fake result with #{package.name}"])
get :index, params: { search_text: package.name }
end

Expand Down
4 changes: 2 additions & 2 deletions src/api/spec/controllers/webui/user_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@

context "when home project creation enabled" do
before do
Configuration.stubs(:allow_user_to_create_home_project).returns(true)
allow(Configuration).to receive(:allow_user_to_create_home_project).and_return(true)
post :register, params: { login: new_user.login, email: new_user.email, password: 'buildservice' }
end

Expand All @@ -232,7 +232,7 @@

context "when home project creation disabled" do
before do
Configuration.stubs(:allow_user_to_create_home_project).returns(false)
allow(Configuration).to receive(:allow_user_to_create_home_project).and_return(false)
post :register, params: { login: new_user.login, email: new_user.email, password: 'buildservice' }
end

Expand Down
4 changes: 2 additions & 2 deletions src/api/spec/features/webui/packages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@
end

scenario "via binaries view" do
Buildresult.stubs(:find_hashed).
allow(Buildresult).to receive(:find_hashed).
with(project: user.home_project, package: package, repository: repository.name, view: %w(binarylist status)).
returns(Xmlhash.parse(fake_buildresult))
and_return(Xmlhash.parse(fake_buildresult))

visit package_binaries_path(project: user.home_project, package: package, repository: repository.name)
click_link("Trigger")
Expand Down
2 changes: 1 addition & 1 deletion src/api/spec/features/webui/projects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
end

scenario "unlock project" do
Project.any_instance.stubs(:can_be_unlocked?).returns(false)
allow_any_instance_of(Project).to receive(:can_be_unlocked?).and_return(false)

click_link("Unlock project")
fill_in "comment", with: "Freedom at last!"
Expand Down
4 changes: 2 additions & 2 deletions src/api/spec/features/webui/sign_up_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

scenario "User with confirmation" do
# Configure confirmation for signups
::Configuration.stubs(:registration).returns("confirmation")
allow_any_instance_of(::Configuration).to receive(:registration).and_return("confirmation")

visit root_path

Expand All @@ -31,7 +31,7 @@

scenario "User is denied" do
# Deny signups
::Configuration.stubs(:registration).returns("deny")
allow_any_instance_of(::Configuration).to receive(:registration).and_return("deny")

visit user_register_user_path

Expand Down
2 changes: 1 addition & 1 deletion src/api/spec/helpers/webui/webui_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@
context 'user is not registered' do
before do
User.current = create(:user)
UnregisteredUser.stubs(:can_register?).raises(APIException)
allow(UnregisteredUser).to receive(:can_register?).and_raise(APIException)
end

it { expect(can_register).to be(false) }
Expand Down
4 changes: 3 additions & 1 deletion src/api/spec/mailers/event_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

RSpec.describe EventMailer do
# Needed for X-OBS-URL
before { Configuration.any_instance.stubs(:obs_url).returns('https://build.example.com') }
before do
allow_any_instance_of(Configuration).to receive(:obs_url).and_return('https://build.example.com')
end

context 'comment mail' do
let!(:receiver) { create(:confirmed_user) }
Expand Down
4 changes: 2 additions & 2 deletions src/api/spec/models/package_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
end

it 'calls #addKiwiImport if filename ends with kiwi.txz' do
Service.any_instance.expects(:addKiwiImport).once
expect_any_instance_of(Service).to receive(:addKiwiImport).once
package.save_file({ filename: 'foo.kiwi.txz' })
end

it 'does not call #addKiwiImport if filename ends not with kiwi.txz' do
Service.any_instance.expects(:addKiwiImport).never
expect_any_instance_of(Service).not_to receive(:addKiwiImport)
package.save_file({ filename: 'foo.spec' })
end
end
Expand Down
Loading

0 comments on commit b6828ae

Please sign in to comment.