Commit 22441dcd397c1ea1e2afdf00c6ab35b479e51d08

Authored by Fellipe Souto Sampaio
Committed by Rafael Manzo
1 parent 9b39be25

Unit test for RepositoriesController refactored at create method

Using lets

Signed-off by: Rafael Reggiani Manzo <rr.manzo@gmail.com>
spec/controllers/repositories_controller_spec.rb
... ... @@ -31,10 +31,11 @@ describe RepositoriesController do
31 31 end
32 32  
33 33 describe 'create' do
  34 + let (:repository) { FactoryGirl.build(:repository, project_id: project.id) }
  35 + let(:repository_params) { Hash[FactoryGirl.attributes_for(:repository).map { |k,v| [k.to_s, v.to_s] }] } #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with symbols and integers
  36 +
34 37 before do
35 38 sign_in FactoryGirl.create(:user)
36   - @subject = FactoryGirl.build(:repository, project_id: project.id)
37   - @subject_params = Hash[FactoryGirl.attributes_for(:repository).map { |k,v| [k.to_s, v.to_s] }] #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with symbols and integers
38 39 end
39 40  
40 41 context 'when the current user owns the project' do
... ... @@ -48,20 +49,19 @@ describe RepositoriesController do
48 49 Repository.any_instance.expects(:process)
49 50 Repository.any_instance.expects(:persisted?).at_least_once.returns(true)
50 51  
51   - post :create, project_id: project.id, repository: @subject_params
  52 + post :create, project_id: project.id, repository: repository_params
52 53 end
53 54  
54   - it { should redirect_to(project_path(@subject.project_id)) }
  55 + it { should redirect_to(project_path(repository.project_id)) }
55 56 it { should respond_with(:redirect) }
56 57 end
57 58  
58 59 context 'with an invalid field' do
59 60 before :each do
60   - Repository.expects(:new).at_least_once.with(@subject_params).returns(@subject)
61 61 Repository.any_instance.expects(:save).returns(false)
62 62 Repository.any_instance.expects(:persisted?).at_least_once.returns(false)
63 63  
64   - post :create, project_id: project.id.to_s, repository: @subject_params
  64 + post :create, project_id: project.id.to_s, repository: repository_params
65 65 end
66 66  
67 67 it { should render_template(:new) }
... ... @@ -70,7 +70,7 @@ describe RepositoriesController do
70 70  
71 71 context "when the current user doesn't owns the project " do
72 72 before :each do
73   - post :create, project_id: project.id, repository: @subject_params
  73 + post :create, project_id: project.id, repository: repository_params
74 74 end
75 75  
76 76 it { should redirect_to(projects_url) }
... ...