最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

New method of Rails controller being called by Rspec test - Stack Overflow

programmeradmin8浏览0评论

I have a Rails controller I'm trying to write a new method for, and I'm running into an issue writing the tests for it. I've simplified the methods and tests repeatedly just trying to get something to pass but no luck. This is why I have now:

def new
    @map_id = 1
    puts @map_id
end
describe 'GET #new' do
  it 'simplified test' do
    get :new
    expect(assigns(:map_id)).to eq(1)
  end
end

The response I get from running this is:

Failure/Error: expect(assigns(:map_id)).to eq(1)
     
expected: 1
got: nil
     
(compared using ==)

I feel like there is something basic I'm missing here that is preventing get :new from correctly working. When I run it from the console, it prints out the 1, so I feel like there is something in the set Rails setup that I'm missing here.

My routes are set and confirmed with rails routes. The files are named correctly and other tests/routes are working fine.

I have a Rails controller I'm trying to write a new method for, and I'm running into an issue writing the tests for it. I've simplified the methods and tests repeatedly just trying to get something to pass but no luck. This is why I have now:

def new
    @map_id = 1
    puts @map_id
end
describe 'GET #new' do
  it 'simplified test' do
    get :new
    expect(assigns(:map_id)).to eq(1)
  end
end

The response I get from running this is:

Failure/Error: expect(assigns(:map_id)).to eq(1)
     
expected: 1
got: nil
     
(compared using ==)

I feel like there is something basic I'm missing here that is preventing get :new from correctly working. When I run it from the console, it prints out the 1, so I feel like there is something in the set Rails setup that I'm missing here.

My routes are set and confirmed with rails routes. The files are named correctly and other tests/routes are working fine.

Share Improve this question asked Mar 24 at 3:05 Dennis ChristmanDennis Christman 114 bronze badges 2
  • 1 Does this test invoke that action? Did you try to use binding.irb inside this action while testing with RSpec? Do you have some code that overrides this instance variable? – mechnicov Commented Mar 24 at 11:39
  • 1 Please show the complete code (ofcourse your simplified version only) in the controller file and the spec file for it to share any insights. – Jignesh Gohel Commented Mar 24 at 12:44
Add a comment  | 

1 Answer 1

Reset to default 0

Using the binding.irb tool mentioned by @mechnicov, I was able to determine that instead of routing to the new method, I was getting an unauthorized viewer message. Turns out there was some legacy authorization code that I needed to account for in my tests.

Specifically, I added the pry gem to my Gemfile, updated the test to this:

describe 'GET #new' do
  it 'simplified test' do
    get :new
    binding.pry
    expect(assigns(:map_id)).to eq(1)
  end
end

Then I ran the test, and examined the response object (which is what this controller is for), then investigated its contents.

发布评论

评论列表(0)

  1. 暂无评论