Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I have some feedback with rspec when writing controllers specs?
    primarykey
    data
    text
    <p>I was wondering if i could have some feedbacks with the controller spec bellow. In fact i'm new when writing specs and controller's spec are way different from model's spec ! So i'm wondering if i may not go in the wrong direction...</p> <p>subjects_controller.rb</p> <pre><code>def show @subject = Subject.find(params[:id]) if @subject.trusted?(current_user) @messages = @subject.messages else @messages = @subject.messages.public @messages = @messages + @subject.messages.where(:user_ids =&gt; current_user.id) @messages.uniq! end # sort the list @messages = @messages.sort_by(&amp;:created_at).reverse if !@subject.company.id == current_user.company.id redirect_to(subjects_path, :notice =&gt; "Invalid subject") end end </code></pre> <p>subjects_controller_spec.rb</p> <pre><code>require 'spec_helper' describe SubjectsController do before(:each) do @subject = mock_model(Subject) end context "for signed users" do before(:each) do @current_user = sign_in Factory(:user) end context "GET #show" do before(:each) do Subject.stub!(:find, @subject).and_return(@subject) end context "when current_user is trusted" do before(:each) do messages = [] company = mock_model(Company) @subject.should_receive(:trusted?).and_return(true) @subject.should_receive(:messages).and_return(messages) @subject.should_receive(:company).and_return(company) end it "should render success" do get :show, :id =&gt; @subject response.should be_success end end context "when current_user is not trusted" do before(:each) do messages = [] company = mock_model(Company) @subject.should_receive(:trusted?).and_return(false) @subject.should_receive(:messages).and_return(messages) messages.should_receive(:public).and_return(messages) @subject.should_receive(:messages).and_return(messages) messages.should_receive(:where).and_return(messages) @subject.should_receive(:company).and_return(company) end it "should render success" do get :show, :id =&gt; @subject response.should be_success end end context "when subject's company is not equal to current_user's company" do # I have no idea of how to implement == end end end end </code></pre> <p>Factories.rb</p> <pre><code>Factory.define :user do |u| u.first_name 'Test User' # u.username 'Test User' # u.surname 'TheTest' # u.email 'foo@foobar.com' # u.password 'please' # u.confirmed_at Time.now # end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload