permalink

My favorite Spec of the day

Here is the controller spec!

def notify
 	@user = User.find(params[:id])
 	if @user.is_expert?
		@topic.experts << @user
	end  @
	user.notify_about_topic!(@topic)
end

And the implementation

it 'should notify user and add to leader board if user is an expert' do  
	user = mock_model(User, :is_expert? => true)  
	topic = mock_model(Topic, :id => 1)  
	collection = mock('col')  
	collection.should_receive(:<<).with(user)  
	topic.should_receive(:experts).and_return(collection)  
	user.should_receive(:notify_about_topic!).with(topic)  
	User.should_receive(:find).with(1).and_return(user)  
	Topic.should_receive(:find).with(1).and_return(topic)  
	post :notify, :topic_id => topic.id, :id => 1, :format => 'js'  
	response.should be_succes  
	assigns(:user).should == user
end

Isn’t this nice to have completely decoupled specs from other outside logic, isn’t Rspec and TDD great?

blog comments powered by Disqus