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
Method: RSpec::Rails::ViewAssigns#assignAssigns a value to an instance variable in the scope of the view being rendered.
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 .
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With