Hello all,
I'm very new to rails and I've been following a lot of great tutorials and examples out there in the community, so first off thank you!
I'm having a problem with my test code. The application works, and I can see from tailing the test logs that the database is being written, but for some reason, it's not returning the save method properly.
This is basically right out of Michael Hartl's Rails Tutorial
The "success" cases are returning an "expected save but received it 0 times", and the "failure case redirects to the wrong place. It almost seems like it's ignoring the if/else block.
Here's the relevant code from the controller spec and the controller
I would really appreciate any insight into this.
Thanks, -Joe
class WebsitesController < ApplicationController
before_filter :require_user, :only => [:new, :edit, :create, :destroy]
def new
@website = Website.new
@title = "Add New Website"
end
def edit
@website = Website.find(params[:id])
@title = "Edit Website"
end
def create
@website = current_user.websites.build(params[:website])
if @website.save
flash[:success] = "Website Added!"
redirect_to(profile_url)
else
render 'new'
@title = "Add New Website"
end
end
def destroy
end
end
require 'spec_helper'
describe WebsitesController do
integrate_views
describe "POST 'create'" do
before(:each) do
activate_authlogic
@user = Factory(:user)
UserSession.create(@user, true)
@attr = {
:domain => "http://www.example.com",
:description => "example site"
}
@website = Factory(:website, @attr.merge(:user => @user))
@user.websites.stub!(:build).and_return(@website)
end
describe "failure" do
before(:each) do
@website.should_receive(:save).and_return(false)
end
it "should render the 'new' page" do
post :create, :website => @attr
response.should render_template('websites/new')
end
end
describe "success" do
before(:each) do
@website.should_receive(:save).and_return(true)
end
it "should redirect to the profile page" do
post :create, :website => @attr
response.should redirect_to(profile_url)
end
it "should have a flash message" do
post :create, :website => @attr
flash[:success].should =~ /website added/i
end
end
end
end