Good day all :-)<br><br>I am getting into RSpec and am a little confused by an aspect of mocks / mock_models in controller tests. I&#39;ve gone through the online docs and the PeepCode movies and have them slightly contradictory on this matter. I hope you can clarify. Many thanks in advance :-)
<br><br>I&#39;m writing a system that uses the restful authentication plugin and am writing a test for the following controller create method:<br><br>class AccountsController &lt; ApplicationController<br>&nbsp; def create<br>
&nbsp;&nbsp;&nbsp; @account = Account.new(params[:account])<br>&nbsp;&nbsp;&nbsp; if @account.save!<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.current_account = @account<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; redirect_back_or_default(&#39;/&#39;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; flash[:notice] = &quot;Thanks for signing up!&quot;<br>
&nbsp;&nbsp;&nbsp; end<br>&nbsp;&nbsp;&nbsp; rescue ActiveRecord::RecordInvalid<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; render :action =&gt; &#39;new&#39;<br>&nbsp; end<br>...<br>end<br><br>I&#39;ve created the following spec test to simply ensure that the Account model receives a call to :new, but the spec is failing as it also receives a call to :save! and it isn&#39;t expecting it. I am using &quot;@account = mock_model(Account)&quot;. If i replace this with &quot;@account = mock(&#39;account&#39;, :null_object =&gt; true)&quot; the test passes, but i&#39;m not sure that&#39;s really what i want. I&#39;m pretty sure i need to us mock_model in this instance.
<br><br>The code is below. <br><br>Also, are my use of comment blocks within the spec against the RSpec *way*. I appreciate that this isn&#39;t using fixtures and i might use them later. I also appreciate that i can write modules to contain these methods, but i can&#39;t quite see the point in muddying things. Comments most welcome.
<br><br>Thanks<br><br>describe AccountsController, &quot;allows users to create only valid accounts&quot; do<br><br>&nbsp; #######&nbsp; <br>&nbsp; ### Reusable methods<br>&nbsp; #######<br><br>&nbsp;&nbsp;&nbsp; def do_post(params = nil)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post :create, :account =&gt; params
<br>&nbsp;&nbsp;&nbsp; end<br>&nbsp; <br>&nbsp;&nbsp;&nbsp; def valid_account_form_details<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { :password =&gt; &quot;password&quot;, :password_confirmation =&gt; &quot;password&quot;, :login =&gt; &quot;new_login&quot;, :email =&gt; &quot;<a href="mailto:test@test.com">
test@test.com</a>&quot; }<br>&nbsp;&nbsp;&nbsp; end<br><br>&nbsp; #######<br>&nbsp; ### Before block<br>&nbsp; #######<br><br>&nbsp;&nbsp;&nbsp; before do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @account = mock_model(Account)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Account.stub!(:new).and_return(@account)<br>&nbsp;&nbsp;&nbsp; end<br><br>&nbsp; #######
<br>&nbsp; ### It Statements<br>&nbsp; #######<br><br>&nbsp;&nbsp;&nbsp; it &quot;should display the new account template when the user visits /accounts/new&quot; do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get &#39;new&#39;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; response.should render_template(:new)<br>&nbsp;&nbsp;&nbsp; end
<br>&nbsp; <br>&nbsp;&nbsp;&nbsp; it &quot;should tell the Account model to create a new account on form POST&quot; do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Account.should_receive(:new).with(:anything).and_return(@account)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; do_post<br>&nbsp;&nbsp;&nbsp; end<br><br>