Reputation: 419
I am trying to implement contact manager with rails. I am using RSpec for test-driven development.
"When I am looking at a single person’s page, I click an add link that takes me to the page where I enter the phone number. I click save, then I see the person and their updated information"
I am trying to validate this action.
In my controller class
describe "POST #create" do
context "with valid params" do
let(:alice) { Person.create(first_name: 'Alice', last_name: 'Smith') }
let(:valid_attributes) { {number: '555-1234', person_id: alice.id} }
it "creates a new PhoneNumber" do
expect {
post :create, params: {phone_number: valid_attributes}, session: valid_session
}.to change(PhoneNumber, :count).by(1)
end
it "redirects to the phone number's person" do
post :create, params: {:phone_number => valid_attributes}, session: valid_session
@phone_number = PhoneNumber.new(person_id: params[:person_id])
expect(response).to redirect_to(@phone_number.person)
end
end
I want to redirect to phone number's person but while getting phone number
It gives me an error undefined local variable or method params' for #<RSpec::ExampleGroups::PhoneNumbersController::POSTCreate::WithValidParams:0x00007fc89e276898>
I also have view test as follows
it 'adds a new phone number' do
page.click_link('Add phone number')
page.fill_in('Number', with: '555-8888')
page.click_button('Create Phone number')
expect(current_path).to eq(person_path(person))
expect(page).to have_content('555-8888')
end
Right now this test is also failing because of this error :
expected: "/people/1"
got: "/phone_numbers"
How can I resolve these problems and redirect user as desired? Thanks in advance
Upvotes: 0
Views: 2186
Reputation: 36860
There's no such thing as params
in your test.
Instead of...
@phone_number = PhoneNumber.new(person_id: params[:person_id])
...you could do...
@phone_number = PhoneNumber.new(person_id: valid_attributes[:person_id])
But since you're just testing that you redirected to the person, you could remove the above line completely and do
expect(response).to redirect_to(alice)
Upvotes: 3