Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialShawn Wilson
iOS Development Techdegree Student 7,049 Points!let undefined method error while trying to run test
hey all im trying to run a spec for the ODOT sample project in ruby basics.... here is the command line error i get:
Failure/Error: let!(:todo_list) { TodoList.create(title: "Grocery list", description: "Groceries") }
NoMethodError:
undefined method let!' for #<RSpec::Core::ExampleGroup::Nested_1:0x007fdc06c90ab0>
# ./spec/features/todo_items/index_spec.rb:5:in
block (2 levels) in <top (required)>'
i have tried to replace the !let with just let, but get same result. i hve retraced the video for a few hours but i am running a newer version of rails so this may be the issue. here is the test code below:
require 'spec_helper'
describe "Viewing todo items" do
it "displays no items when a todo list is empty" do
let!(:todo_list) { TodoList.create(Title: "Grocery list", Description: "Groceries") }
visit "/todo_lists"
witih "#todo_list_#{todo_list.id}" do
click_link "List Items"
end
expect(page).to have_content("TodoItems#index")
end
end
im trying to read the error but cant quite grasp it i know it tells me the problem in the error message and i know it is boiling down to the let! but not sure how to proceed. ive tried to refrence google and rails guide but to no avail. is this likely an issue that im running a newer version of ROR over what is being shown in the demonstration?
1 Answer
Maciej Czuchnowski
36,441 PointsThese let
statements should not be placed inside the it
blocks, only inside the higher level describe blocks. Just move it two lines up and it should work :)
Shawn Wilson
iOS Development Techdegree Student 7,049 Pointsso do i move the entire line up two lines above the it block or just the let!? is this something that has changed between versions of rails? because in the tutorial it is where i have it placed now and the test performs just fine.
just trying to understand how the syntax has evolved.
Maciej Czuchnowski
36,441 PointsWell, I just tested this in my own app. I got this error:
let` is not available from within an example (e.g. an `it` block) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). It is only available on an example group (e.g. a `describe` or `context` block).
I use rspec-rails 3.2.1. So this runs fine, since the whole "let" is within the describe block:
describe "GET #index" do
let(:super_product) { Product.create(title: "Super Sledge", description: "Preferred melee weapon of supermutants", price: 99.99, image_url: "sledge.jpg") }
it "assigns a @products variable" do
product = Product.create!(valid_attributes)
get :index
expect(assigns(:products)).to eq([product, super_product])
end
end
But this gives me the error I quoted because it's in the it
block:
describe "GET #index" do
it "assigns a @products variable" do
let(:super_product) { Product.create(title: "Super Sledge", description: "Preferred melee weapon of supermutants", price: 99.99, image_url: "sledge.jpg") }
product = Product.create!(valid_attributes)
get :index
expect(assigns(:products)).to eq([product, super_product])
end
end
Maciej Czuchnowski
36,441 PointsI you're serious about learning RSpec, you should check out this book: https://leanpub.com/everydayrailsrspec
Bear in mind that the creator of Rails hates Rspec and the whole "Prime stack":
http://words.steveklabnik.com/rails-has-two-default-stacks
http://www.rubyinside.com/dhh-offended-by-rspec-debate-4610.html
Shawn Wilson
iOS Development Techdegree Student 7,049 PointsShawn Wilson
iOS Development Techdegree Student 7,049 Pointsshould you wish to look at the entire project for refrence i can be found on Git under the name swilson223 cheers,