# Testing User Agent

**URL:** <https://www.ruby-forum.com/t/testing-user-agent/161302>\
**Category:** RSpec\
**Created:** [February 25, 2009, 9:02am UTC](https://www.ruby-forum.com/t/testing-user-agent/161302 "2009-02-25T09:02:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suprie\_Leonhart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@Suprie\_Leonhart](https://www.ruby-forum.com/u/Suprie_Leonhart)\
**Post date:** [February 25, 2009, 9:02am UTC](https://www.ruby-forum.com/t/testing-user-agent/161302/1 "2009-02-25T09:02:36Z")

</div>

hi

i’m testing some lib i’ve made for detecting mobile user agent, I  
confused  
how to test the lib i’ve made.  
the test code is

```auto

  describe "Check if mobile browser" do
    it "should know if it's from blackberry" do
      blackberry_ua = "BlackBerry8330/4.3.0 Profile/MIDP-2.0
Configuration/CLDC-1.1 VendorID/105"
      request.env["HTTP_USER_AGENT"] = blackberry_ua
      is_mobile?.should == true
    end
  end

```

the error is, request variable always nil

the question : how i mock the request.env ?

## –

Best Regards  
Suprie  
[http://suprie.in.ruangkopi.com](http://suprie.in.ruangkopi.com)

“Mendapat upah karena menyenangkan orang lain yang tidak punya  
persangkutan  
dengan kata hati sendiri, kan itu dalam seni namanya pelacuran?”

---

<div class="post-metadata">

**Author:** ![Suprie\_Leonhart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@Suprie\_Leonhart](https://www.ruby-forum.com/u/Suprie_Leonhart)\
**Post date:** [February 25, 2009, 10:44am UTC](https://www.ruby-forum.com/t/testing-user-agent/161302/2 "2009-02-25T10:44:24Z")

</div>

On Wed, Feb 25, 2009 at 1:57 AM, Suprie Leonhart  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> blackberry_ua = "BlackBerry8330/4.3.0 Profile/MIDP-2.0
> 
> ```
> 
> the question : how i mock the request.env ?  
> Since you’re trying to test existing code, can you please post that  
> code?

---

<div class="post-metadata">

**Author:** ![Suprie\_Leonhart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@Suprie\_Leonhart](https://www.ruby-forum.com/u/Suprie_Leonhart)\
**Post date:** [February 25, 2009, 10:53am UTC](https://www.ruby-forum.com/t/testing-user-agent/161302/3 "2009-02-25T10:53:49Z")

</div>

On Wed, Feb 25, 2009 at 4:19 PM, David C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > the error is, request variable always nil
> > 
> > the question : how i mock the request.env ?
> 
> Since you’re trying to test existing code, can you please post that code?

```auto
module Mobile
  def load_mobile_agent_file
    ma = File.read(RAILS_ROOT+"/config/mobile.yml")
    yaml = YAML.load(ma)
    return yaml
  end

  def mobile_user_agent?
    if request.env["HTTP_USER_AGENT"]
      user_agent = request.env["HTTP_USER_AGENT"]
      mua = load_mobile_agent_file
      mua["mobile_agent"].each do |key, value|
        unless(user_agent.match(Regexp.new(Regexp.escape(key))).nil?)
          return true
        end
      end
      return false
    end
  end
end
```

## –

Best Regards  
Suprie  
[http://suprie.in.ruangkopi.com](http://suprie.in.ruangkopi.com)

“Mendapat upah karena menyenangkan orang lain yang tidak punya  
persangkutan  
dengan kata hati sendiri, kan itu dalam seni namanya pelacuran?”

---

<div class="post-metadata">

**Author:** ![Suprie\_Leonhart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@Suprie\_Leonhart](https://www.ruby-forum.com/u/Suprie_Leonhart)\
**Post date:** [February 25, 2009, 10:53pm UTC](https://www.ruby-forum.com/t/testing-user-agent/161302/4 "2009-02-25T22:53:39Z")

</div>

On 25/02/2009, at 4:51 AM, Suprie Leonhart wrote:

> > 
> 
> ## module Mobile mua[“mobile\_agent”].each do |key, value|
> 
> –  
> Best Regards  
> Suprie  
> [http://suprie.in.ruangkopi.com](http://suprie.in.ruangkopi.com)

Hi Suprie. AFAIK, RSpec populates the “request” object in your specs  
after a controller action is called. Since your “should know if it’s  
from blackberry” example doesn’t call a controller action, the  
“request” object is nil.  
-Nick

---

<div class="post-metadata">

**Author:** ![Suprie\_Leonhart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@Suprie\_Leonhart](https://www.ruby-forum.com/u/Suprie_Leonhart)\
**Post date:** [February 26, 2009, 3:55am UTC](https://www.ruby-forum.com/t/testing-user-agent/161302/5 "2009-02-26T03:55:02Z")

</div>

On Wed, Feb 25, 2009 at 10:45 PM, David C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > > > Configuration/CLDC-1.1 VendorID/105"
> 
> > ```
> > user_agent = request.env["HTTP_USER_AGENT"]
> > 
> > ```
> > 
> > [/code]
> 
> Thanks - so I’m wondering where is\_mobile? came from. Also, are you  
> trying to do this in a controller spec or a helper spec? Where is the  
> spec file located?

i’ve refactoring the is\_mobile? to mobile\_content? i’m trying to spec a  
lib, so i put it on spec/lib

## –

Best Regards  
Suprie  
[http://suprie.in.ruangkopi.com](http://suprie.in.ruangkopi.com)

“Mendapat upah karena menyenangkan orang lain yang tidak punya  
persangkutan  
dengan kata hati sendiri, kan itu dalam seni namanya pelacuran?”

---

<div class="post-metadata">

**Author:** ![Suprie\_Leonhart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@Suprie\_Leonhart](https://www.ruby-forum.com/u/Suprie_Leonhart)\
**Post date:** [February 25, 2009, 4:47pm UTC](https://www.ruby-forum.com/t/testing-user-agent/161302/6 "2009-02-25T16:47:25Z")

</div>

On Wed, Feb 25, 2009 at 3:51 AM, Suprie Leonhart  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > confused  
> > > is\_mobile?.should == true
> 
> [code]  
> module Mobile  
> def load\_mobile\_agent\_file  
> ma = File.read(RAILS\_ROOT+“/config/mobile.yml”)  
> yaml = YAML.load(ma)  
> return yaml  
> end

Off topic, but this could be a one liner with no temporary variables,  
and load\_file will take care of opening and reading the file for you:

def load\_mobile\_agent\_file  
YAML.load\_file(RAILS\_ROOT+“/config/mobile.yml”)  
end

> ```
> return false
> end
> 
> ```
> 
> end  
> end  
> [/code]

Thanks - so I’m wondering where is\_mobile? came from. Also, are you  
trying to do this in a controller spec or a helper spec? Where is the  
spec file located?

---

<div class="post-metadata">

**Author:** ![Suprie\_Leonhart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@Suprie\_Leonhart](https://www.ruby-forum.com/u/Suprie_Leonhart)\
**Post date:** [February 26, 2009, 4:23am UTC](https://www.ruby-forum.com/t/testing-user-agent/161302/7 "2009-02-26T04:23:45Z")

</div>

On Thu, Feb 26, 2009 at 4:39 AM, Nick H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi Suprie. AFAIK, RSpec populates the “request” object in your specs after  
> a controller action is called. Since your “should know if it’s from  
> blackberry” example doesn’t call a controller action, the “request” object  
> is nil.  
> -Nick

so i should put the test on controller spec then ?

## –

Best Regards  
Suprie  
[http://suprie.in.ruangkopi.com](http://suprie.in.ruangkopi.com)

“Mendapat upah karena menyenangkan orang lain yang tidak punya  
persangkutan  
dengan kata hati sendiri, kan itu dalam seni namanya pelacuran?”

---

<div class="post-metadata">

**Author:** ![Suprie\_Leonhart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@Suprie\_Leonhart](https://www.ruby-forum.com/u/Suprie_Leonhart)\
**Post date:** [February 26, 2009, 7:15am UTC](https://www.ruby-forum.com/t/testing-user-agent/161302/8 "2009-02-26T07:15:09Z")

</div>

On Thu, Feb 26, 2009 at 12:08 PM, Nick H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > 
> 
> specs for that action, and make sure to cover the case(s) when  
> #mobile\_content? is called.

thanks a lot for the advice.

## –

Best Regards  
Suprie  
[http://suprie.in.ruangkopi.com](http://suprie.in.ruangkopi.com)

“Mendapat upah karena menyenangkan orang lain yang tidak punya  
persangkutan  
dengan kata hati sendiri, kan itu dalam seni namanya pelacuran?”

---

<div class="post-metadata">

**Author:** ![Suprie\_Leonhart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@Suprie\_Leonhart](https://www.ruby-forum.com/u/Suprie_Leonhart)\
**Post date:** [February 26, 2009, 6:16am UTC](https://www.ruby-forum.com/t/testing-user-agent/161302/9 "2009-02-26T06:16:04Z")

</div>

On 25/02/2009, at 9:49 PM, Suprie Leonhart wrote:

> ## –
> 
> Best Regards  
> Suprie

Ultimately, you should be speccing your application’s behaviour rather  
than specific methods. If a controller action calls #mobile\_content? ,  
then write specs for that action, and make sure to cover the case(s)  
when #mobile\_content? is called.  
-Nick
