问题
Let say I have setup like this:
config/routes.rb
resources :graves do
resources :candles
end
app/models/grave.rb
has_many :candles, dependent: :destroy
app/models/candles.rb
belongs_to :grave
app/controllers/candles_controller.rb
before_action :get_grave
def create
@candle = @grave.candles.new(candle_params)
respond_to do |format|
if @candle.save
format.html {
redirect_to @grave, notice: 'Candle was successfully created.'
}
format.json { }
else
format.html { render :new }
format.json { render json: @candle.errors, status: :unprocessable_entity }
end
end
end
private
def candle_params
params.require(:candle).permit(:name, :body)
end
def get_grave
@grave = Grave.find(params[:grave_id])
end
spec/controllers/candles_controller_spec.rb
describe CandlesController, type: :controller do
describe "POST create" do
before(:each) do
@grave = FactoryGirl.create(:grave)
end
it "creates candle" do
post :create, candle: { name: "Dummy name", body: "Dummy body", grave_id: @grave.id}
expect(response).to redirect_to(@grave)
end
end
end
Problem is when I run this spec I get this error:
1) CandlesController POST create creates candle Failure/Error: expect(response).to redirect_to(@grave) Expected response to be a < redirect >, but was <404>
I've checked FactoryGirl and @grave
is created so it's not nil. When I test manually candle
is created correctly and it's associated to grave
. Any clues what may cause spec to fail?
Rails 4.2, RSpec 3.x
回答1:
Your route will look like this: graves/:grave_id/candles
, so you have to pass a @grave.id
to your post
method as grave_id
.
post :create, grave_id: @grave.id, candle: { name: "Dummy name", body: "Dummy body", grave_id: @grave.id}
来源:https://stackoverflow.com/questions/37399068/rspec-controller-for-nested-resources-before-action