Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does assigns mean in rspec

Tags:

What does that line of code do?

assigns(:articles).should eq([article]) 

in the following rspec

  describe "GET #index" do     it "populates an array of articles" do       article = Factory(:article)       get :index       assigns(:articles).should eq([article])     end      it "renders the :index view" do       get :index       response.should render_template :index     end   end 
like image 979
Aydar Omurbekov Avatar asked Apr 21 '13 16:04

Aydar Omurbekov


People also ask

What is assigns RSpec?

Method: RSpec::Rails::ViewAssigns#assignAssigns a value to an instance variable in the scope of the view being rendered.

What does let means in RSpec?

let generates a method whose return value is memoized after the first call. This is known as lazy loading because the value is not loaded into memory until the method is called. Here is an example of how let is used within an RSpec test. let will generate a method called thing which returns a new instance of Thing .

How do I set an instance variable in RSpec?

If you want to set a controller or view's instance variables in your RSpec test, then call assign either in a before block or at the start of an example group. The first argument is the name of the instance variable while the second is the value you want to assign to it.

What is Rails RSpec?

RSpec is a behavior-driven development (BDD) testing tool for Ruby, and is widely used for testing both plain ol' Ruby and full-on Rails applications.


1 Answers

assigns relates to the instance variables created within a controller action (and assigned to the view).


to answer your remark in comments, I guess that:

  • 1) your index action looks like @articles = Articles.all (I hope you use pagination though)

  • 2) prior to the spec block above, you have one article created in db (or I hope you stub db queries in db)

  • 1 + 2 => @articles should contain one article, that's your spec expectation

like image 63
apneadiving Avatar answered Sep 21 '22 09:09

apneadiving