RSpec test for handling known IP addresses

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I want to replace the array of ips with a method (like known_ips or just ips) but I don't know how.



I'll use this known_ips somewhere else.



describe WelcomeController, type: :controller do
[

ip: '73.53.61.23',
location_name: 'Seattle, Washington, US'
,
ip: '8.26.157.16',
location_name: 'San Francisco, California, US'
,
ip: '174.112.13.21',
location_name: 'Mississauga, Ontario, CA'

].each do |params|
it 'geolocalizes the visitor based on the ip' do
allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip).and_return params[:ip]
get :index
expect(controller.current_location.name).to eq params[:location_name]
end
end
end






share|improve this question





















  • You should include your other code as well so that we can see what they have in common.
    – 200_success
    Mar 1 at 12:51










  • Since you are really asking for a solution to a problem and not a code review you should post this on Srack Overflow instead. In the spirit of code review, I would replace the very generic allow_any_instance_of(ActionDispatch::Request).to_receive with expect(controller.request).to_receive
    – Marc Rohloff
    Mar 1 at 17:25
















up vote
1
down vote

favorite












I want to replace the array of ips with a method (like known_ips or just ips) but I don't know how.



I'll use this known_ips somewhere else.



describe WelcomeController, type: :controller do
[

ip: '73.53.61.23',
location_name: 'Seattle, Washington, US'
,
ip: '8.26.157.16',
location_name: 'San Francisco, California, US'
,
ip: '174.112.13.21',
location_name: 'Mississauga, Ontario, CA'

].each do |params|
it 'geolocalizes the visitor based on the ip' do
allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip).and_return params[:ip]
get :index
expect(controller.current_location.name).to eq params[:location_name]
end
end
end






share|improve this question





















  • You should include your other code as well so that we can see what they have in common.
    – 200_success
    Mar 1 at 12:51










  • Since you are really asking for a solution to a problem and not a code review you should post this on Srack Overflow instead. In the spirit of code review, I would replace the very generic allow_any_instance_of(ActionDispatch::Request).to_receive with expect(controller.request).to_receive
    – Marc Rohloff
    Mar 1 at 17:25












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I want to replace the array of ips with a method (like known_ips or just ips) but I don't know how.



I'll use this known_ips somewhere else.



describe WelcomeController, type: :controller do
[

ip: '73.53.61.23',
location_name: 'Seattle, Washington, US'
,
ip: '8.26.157.16',
location_name: 'San Francisco, California, US'
,
ip: '174.112.13.21',
location_name: 'Mississauga, Ontario, CA'

].each do |params|
it 'geolocalizes the visitor based on the ip' do
allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip).and_return params[:ip]
get :index
expect(controller.current_location.name).to eq params[:location_name]
end
end
end






share|improve this question













I want to replace the array of ips with a method (like known_ips or just ips) but I don't know how.



I'll use this known_ips somewhere else.



describe WelcomeController, type: :controller do
[

ip: '73.53.61.23',
location_name: 'Seattle, Washington, US'
,
ip: '8.26.157.16',
location_name: 'San Francisco, California, US'
,
ip: '174.112.13.21',
location_name: 'Mississauga, Ontario, CA'

].each do |params|
it 'geolocalizes the visitor based on the ip' do
allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip).and_return params[:ip]
get :index
expect(controller.current_location.name).to eq params[:location_name]
end
end
end








share|improve this question












share|improve this question




share|improve this question








edited Mar 1 at 12:49









200_success

123k14142399




123k14142399









asked Mar 1 at 12:14









Pedro Bernardes

1084




1084











  • You should include your other code as well so that we can see what they have in common.
    – 200_success
    Mar 1 at 12:51










  • Since you are really asking for a solution to a problem and not a code review you should post this on Srack Overflow instead. In the spirit of code review, I would replace the very generic allow_any_instance_of(ActionDispatch::Request).to_receive with expect(controller.request).to_receive
    – Marc Rohloff
    Mar 1 at 17:25
















  • You should include your other code as well so that we can see what they have in common.
    – 200_success
    Mar 1 at 12:51










  • Since you are really asking for a solution to a problem and not a code review you should post this on Srack Overflow instead. In the spirit of code review, I would replace the very generic allow_any_instance_of(ActionDispatch::Request).to_receive with expect(controller.request).to_receive
    – Marc Rohloff
    Mar 1 at 17:25















You should include your other code as well so that we can see what they have in common.
– 200_success
Mar 1 at 12:51




You should include your other code as well so that we can see what they have in common.
– 200_success
Mar 1 at 12:51












Since you are really asking for a solution to a problem and not a code review you should post this on Srack Overflow instead. In the spirit of code review, I would replace the very generic allow_any_instance_of(ActionDispatch::Request).to_receive with expect(controller.request).to_receive
– Marc Rohloff
Mar 1 at 17:25




Since you are really asking for a solution to a problem and not a code review you should post this on Srack Overflow instead. In the spirit of code review, I would replace the very generic allow_any_instance_of(ActionDispatch::Request).to_receive with expect(controller.request).to_receive
– Marc Rohloff
Mar 1 at 17:25










1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted











I'll use this known_ips somewhere else.




I'm guessing that "somewhere" will still be in a test file. If so you can put this in support file (which is a common patern in RSpec).



# spec/support/ip_list.rb
IP_LIST = [
# easily to recognize that IP address is first and the location name the second, but you can use your code that uses Hash here
['73.53.61.23', 'Seattle, Washington, US'],
['8.26.157.16', 'San Francisco, California, US'],
['174.112.13.21', 'Mississauga, Ontario, CA']
]


Then on spec_helper.rb (see this example)



# spec/spec_helper.rb
# after the last require
Dir[Rails.root.join("spec/support/**/*.rb")].each require f


And in your actual test:



describe WelcomeController, type: :controller do
it 'geolocalizes the visitor based on the ip' do
IP_LIST.each do |(ip, location_name)|
expect(controller.request).to receive(:remote_ip).and_return ip
get :index
expect(controller.current_location.name).to eq location_name
end
end
end


Note that I nested IP_LIST.each inside the it block, I don't think you need to create one it for each test. This is a personal opinion, I'm not sure what is the best practices here.






share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188598%2frspec-test-for-handling-known-ip-addresses%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted











    I'll use this known_ips somewhere else.




    I'm guessing that "somewhere" will still be in a test file. If so you can put this in support file (which is a common patern in RSpec).



    # spec/support/ip_list.rb
    IP_LIST = [
    # easily to recognize that IP address is first and the location name the second, but you can use your code that uses Hash here
    ['73.53.61.23', 'Seattle, Washington, US'],
    ['8.26.157.16', 'San Francisco, California, US'],
    ['174.112.13.21', 'Mississauga, Ontario, CA']
    ]


    Then on spec_helper.rb (see this example)



    # spec/spec_helper.rb
    # after the last require
    Dir[Rails.root.join("spec/support/**/*.rb")].each require f


    And in your actual test:



    describe WelcomeController, type: :controller do
    it 'geolocalizes the visitor based on the ip' do
    IP_LIST.each do |(ip, location_name)|
    expect(controller.request).to receive(:remote_ip).and_return ip
    get :index
    expect(controller.current_location.name).to eq location_name
    end
    end
    end


    Note that I nested IP_LIST.each inside the it block, I don't think you need to create one it for each test. This is a personal opinion, I'm not sure what is the best practices here.






    share|improve this answer

























      up vote
      1
      down vote



      accepted











      I'll use this known_ips somewhere else.




      I'm guessing that "somewhere" will still be in a test file. If so you can put this in support file (which is a common patern in RSpec).



      # spec/support/ip_list.rb
      IP_LIST = [
      # easily to recognize that IP address is first and the location name the second, but you can use your code that uses Hash here
      ['73.53.61.23', 'Seattle, Washington, US'],
      ['8.26.157.16', 'San Francisco, California, US'],
      ['174.112.13.21', 'Mississauga, Ontario, CA']
      ]


      Then on spec_helper.rb (see this example)



      # spec/spec_helper.rb
      # after the last require
      Dir[Rails.root.join("spec/support/**/*.rb")].each require f


      And in your actual test:



      describe WelcomeController, type: :controller do
      it 'geolocalizes the visitor based on the ip' do
      IP_LIST.each do |(ip, location_name)|
      expect(controller.request).to receive(:remote_ip).and_return ip
      get :index
      expect(controller.current_location.name).to eq location_name
      end
      end
      end


      Note that I nested IP_LIST.each inside the it block, I don't think you need to create one it for each test. This is a personal opinion, I'm not sure what is the best practices here.






      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted







        I'll use this known_ips somewhere else.




        I'm guessing that "somewhere" will still be in a test file. If so you can put this in support file (which is a common patern in RSpec).



        # spec/support/ip_list.rb
        IP_LIST = [
        # easily to recognize that IP address is first and the location name the second, but you can use your code that uses Hash here
        ['73.53.61.23', 'Seattle, Washington, US'],
        ['8.26.157.16', 'San Francisco, California, US'],
        ['174.112.13.21', 'Mississauga, Ontario, CA']
        ]


        Then on spec_helper.rb (see this example)



        # spec/spec_helper.rb
        # after the last require
        Dir[Rails.root.join("spec/support/**/*.rb")].each require f


        And in your actual test:



        describe WelcomeController, type: :controller do
        it 'geolocalizes the visitor based on the ip' do
        IP_LIST.each do |(ip, location_name)|
        expect(controller.request).to receive(:remote_ip).and_return ip
        get :index
        expect(controller.current_location.name).to eq location_name
        end
        end
        end


        Note that I nested IP_LIST.each inside the it block, I don't think you need to create one it for each test. This is a personal opinion, I'm not sure what is the best practices here.






        share|improve this answer














        I'll use this known_ips somewhere else.




        I'm guessing that "somewhere" will still be in a test file. If so you can put this in support file (which is a common patern in RSpec).



        # spec/support/ip_list.rb
        IP_LIST = [
        # easily to recognize that IP address is first and the location name the second, but you can use your code that uses Hash here
        ['73.53.61.23', 'Seattle, Washington, US'],
        ['8.26.157.16', 'San Francisco, California, US'],
        ['174.112.13.21', 'Mississauga, Ontario, CA']
        ]


        Then on spec_helper.rb (see this example)



        # spec/spec_helper.rb
        # after the last require
        Dir[Rails.root.join("spec/support/**/*.rb")].each require f


        And in your actual test:



        describe WelcomeController, type: :controller do
        it 'geolocalizes the visitor based on the ip' do
        IP_LIST.each do |(ip, location_name)|
        expect(controller.request).to receive(:remote_ip).and_return ip
        get :index
        expect(controller.current_location.name).to eq location_name
        end
        end
        end


        Note that I nested IP_LIST.each inside the it block, I don't think you need to create one it for each test. This is a personal opinion, I'm not sure what is the best practices here.







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Mar 2 at 19:32









        Peoplee

        20514




        20514






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188598%2frspec-test-for-handling-known-ip-addresses%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Greedy Best First Search implementation in Rust

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            C++11 CLH Lock Implementation