This is what rails test:controllers gets me.
Failure:
StoriesControllerTest#test_adds_a_story [test/controllers/stories_controller_test.rb:42]:
Expected response to be a redirect to <http://www.example.com/stories/980190962> but was a redirect to <http://www.example.com/stories/980190963>.
Expected "http://www.example.com/stories/980190962" to be === "http://www.example.com/stories/980190963".
It seems to be offsetting the story url by one
This is the specific test that fails:
assert_redirected_to story_url(@story)
I tried changing what the test should say but that leads to either errors or failures.
here is the test that fails:
test "adds a story" do
assert_difference "Story.count" do
post stories_path, params: {
story: {
name: 'test story',
link: 'http://www.test.com/'
}
}
end
assert_redirected_to story_url(@story)
assert_not_nil flash[:notice]
end
and here is the controller action
def create
@story = Story.new(story_params)
respond_to do |format|
if @story.save
format.html { redirect_to story_url(@story), notice: "Story was successfully created." }
format.json { render :show, status: :created, location: @story }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @story.errors, status: :unprocessable_entity }
end
end
end
As you already mentioned in the comments, when using
setup do
@story = stories(:one)
end
then the one
story that was created by the fixtures is assigned to @story
and not the story that was just created during the test.
Instead, I suggest changing your test to:
test "adds a story" do
assert_difference "Story.count" do
post stories_path,
params: { story: { name: 'test story', link: 'http://www.test.com/' } }
end
assert_redirected_to story_url(Story.last)
assert_equal "Story was successfully created.", flash[:notice]
end