Skip to content

Commit

Permalink
Fix broken build
Browse files Browse the repository at this point in the history
  • Loading branch information
marclerodrigues committed Dec 8, 2019
1 parent de55851 commit c4a46f6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 38 deletions.
6 changes: 3 additions & 3 deletions app/controllers/entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def create
format.json { render :show, status: :created, location: @entry }
else
flash.now[:error] = 'Sorry, an error has occured'
format.html { render :new }
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @entry.errors, status: :unprocessable_entity }
end
end
Expand All @@ -49,7 +49,7 @@ def update
format.json { render :show, status: :ok, location: @entry }
else
flash.now[:error] = 'Sorry, an error has occured'
format.html { render :edit }
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @entry.errors, status: :unprocessable_entity }
end
end
Expand All @@ -74,6 +74,6 @@ def set_entry

# Never trust parameters from the scary internet, only allow the white list through.
def entry_params
params.require(:entry).permit(:feeling, :description, :day, :hour)
params.require(:entry).permit(:feeling, :description, :day, :hour, :user_id)
end
end
62 changes: 28 additions & 34 deletions spec/controllers/entries_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
# This should return the minimal set of attributes required to create a valid
# Entry. As you add validations to Entry, be sure to
# adjust the attributes here as well.
let(:user) { create(:user) }
let(:valid_attributes) {
{ feeling: 'awesome', description: 'WoooWW', hour: 22 }
attributes_for(:entry, user_id: user.id)
}

let(:invalid_attributes) {
{ other_feeling: 'bad mood', age: 66, admin: true }
attributes_for(:entry, user_id: nil)
}

# This should return the minimal set of values that should be in the session
Expand All @@ -42,16 +43,20 @@
let(:valid_session) { {} }

describe "GET #index" do
before do
create(:entry, valid_attributes)
end

it "returns a success response" do
Entry.create! valid_attributes
get :index, params: {}, session: valid_session
expect(response).to be_successful
end
end

describe "GET #show" do
let(:entry) { create(:entry, valid_attributes) }

it "returns a success response" do
entry = Entry.create! valid_attributes
get :show, params: {id: entry.to_param}, session: valid_session
expect(response).to be_successful
end
Expand All @@ -65,9 +70,10 @@
end

describe "GET #edit" do
let(:entry) { create(:entry, valid_attributes) }

it "returns a success response" do
entry = Entry.create! valid_attributes
get :edit, params: {id: entry.to_param}, session: valid_session
get :edit, params: { id: entry.to_param }, session: valid_session
expect(response).to be_successful
end
end
Expand All @@ -76,45 +82,41 @@
context "with valid params" do
it "creates a new Entry" do
expect {
post :create, params: {entry: valid_attributes}, session: valid_session
post :create, params: { entry: valid_attributes }, session: valid_session
}.to change(Entry, :count).by(1)
end

it "redirects to the created entry" do
post :create, params: {entry: valid_attributes}, session: valid_session
post :create, params: { entry: valid_attributes }, session: valid_session
expect(response).to redirect_to(Entry.last)
end

it "render success flash message" do
post :create, params: {entry: valid_attributes}, session: valid_session
post :create, params: { entry: valid_attributes }, session: valid_session
expect(flash[:success]).to_not be_nil
end

it "render success flash message text" do
post :create, params: {entry: valid_attributes}, session: valid_session
post :create, params: { entry: valid_attributes }, session: valid_session
expect(flash[:success]).to match(/Entry was successfully created./)
end
end

context "with invalid params" do
it "returns a failure response (i.e. to display the 'new' template)" do
post :create, params: {entry: invalid_attributes}, session: valid_session
post :create, params: { entry: invalid_attributes }, session: valid_session
expect(response).not_to be_successful
end

it "render failure flash message" do
skip('Missing template')

expect_any_instance_of(Entry).to receive(:save).and_return(false)
post :create, params: {entry: invalid_attributes}, session: valid_session
post :create, params: { entry: invalid_attributes }, session: valid_session
expect(flash[:error]).to_not be_nil
end

it "render failure flash message text" do
skip('Missing template')

expect_any_instance_of(Entry).to receive(:save).and_return(false)
post :create, params: {entry: invalid_attributes}, session: valid_session
post :create, params: { entry: invalid_attributes }, session: valid_session
expect(flash[:error]).to match(/Sorry, an error has occured/)
end
end
Expand All @@ -123,54 +125,46 @@
describe "PUT #update" do
context "with valid params" do
let(:new_attributes) {
skip("Add a hash of attributes valid for your model")
attributes_for(:entry, feeling: 'Good', user_id: user.id)
}
let(:entry) { create(:entry, user_id: user.id) }

it "updates the requested entry" do
entry = Entry.create! valid_attributes
put :update, params: {id: entry.to_param, entry: new_attributes}, session: valid_session
put :update, params: { id: entry.to_param, entry: new_attributes }, session: valid_session
entry.reload
skip("Add assertions for updated state")
expect(entry.feeling).to eq('Good')
end

it "redirects to the entry" do
entry = Entry.create! valid_attributes
put :update, params: {id: entry.to_param, entry: valid_attributes}, session: valid_session
put :update, params: { id: entry.to_param, entry: valid_attributes }, session: valid_session
expect(response).to redirect_to(entry)
end

it "render success flash message" do
entry = Entry.create! valid_attributes
put :update, params: {id: entry.to_param, entry: valid_attributes}, session: valid_session
put :update, params: { id: entry.to_param, entry: valid_attributes }, session: valid_session
expect(flash[:success]).to_not be_nil
end

it "render success flash message text" do
entry = Entry.create! valid_attributes
put :update, params: {id: entry.to_param, entry: valid_attributes}, session: valid_session
put :update, params: { id: entry.to_param, entry: valid_attributes }, session: valid_session
expect(flash[:success]).to match(/Entry was successfully updated./)
end
end

context "with invalid params" do
let(:entry) { create(:entry, valid_attributes) }

it "returns a success response (i.e. to display the 'edit' template)" do
entry = Entry.create! valid_attributes
put :update, params: {id: entry.to_param, entry: invalid_attributes}, session: valid_session
expect(response).not_to be_successful
end

it "render failure flash message" do
skip('Missing template')

entry = Entry.create! valid_attributes
put :update, params: {id: entry.to_param, entry: invalid_attributes}, session: valid_session
expect(flash[:error]).to_not be_nil
end

it "render failure flash message text" do
skip('Missing template')

entry = Entry.create! valid_attributes
put :update, params: {id: entry.to_param, entry: invalid_attributes}, session: valid_session
expect(flash[:error]).to match(/Sorry, an error has occured/)
end
Expand Down
3 changes: 2 additions & 1 deletion spec/factories/entries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
description { Faker::Lorem.words(number: 10) }
hour { Time.now }
day { Date.today}
user
end
end
end

0 comments on commit c4a46f6

Please sign in to comment.